3

Is it possible to call private functions declared in another class ? If yes, I need to call a function inside my main file. This function has to be imported from another module. This function is defined inside a class as follows.

class ConfLoader(object):
.....
    def _set_config_value(self, section, attribute, value):
....

Any suggestions ?

Joseph Wahba
  • 337
  • 2
  • 8
  • 19
  • 1
    There is no private functions or methods in Python, are you using any Framework ? – Arount Nov 28 '16 at 20:15
  • your function starts by a single underscore. You can call it normally. – Jean-François Fabre Nov 28 '16 at 20:18
  • While it's true that there are no true private methods in Python, the leading underscore here suggests that you shouldn't be calling this function yourself. Why do you think you need to ? – ChrisGPT was on strike Nov 28 '16 at 20:18
  • 1
    Yes. There aren't really private methods in Python, just conventions that certain things should be treated as though they are private. You can do it exactly like you would call any other method from another module, but just be aware that whoever wrote that other module expected you to not use that functionality, so it may not behave the way you expect it to. – Patrick Haugh Nov 28 '16 at 20:18
  • You still need to instantiate the class and call the method on the instance. – Alex Hall Nov 28 '16 at 20:19

2 Answers2

12

Python does not have "enforced" private functions, but it does have a naming convention that makes it harder to do accidentally. This is usually done by prepending a double underscore (not, as in your example, a single one, which functions as a normal method name). For example:

class X(object):
    def __f(self):
        print('hello')

If I attempt to use the same name this outside the class definition, it will fail:

>>> x = X()
>>> x.__f()
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    x.__f()
AttributeError: 'X' object has no attribute '__f'

Instead, Python uses _ClassName__PrivateMethod. I can still call this pseudo-private function outside the class definition using the following:

>>> x._X__f()
hello

See Why are Python's 'private' methods not actually private? for some more useful information.

Note, of course, that just because something is possible does not mean it is a good idea. There is a reason why a well-designed class has private functions: these are internals that are not meant to be safely interacted with by outside functions.

Community
  • 1
  • 1
brianpck
  • 8,084
  • 1
  • 22
  • 33
0

Python implements psuedo-private methods by munging method names that start with two underscores but don't end with underscores. It Just adds the class name to the front.

>>> class Foo(object):
...     def __private(self):
...         print('secret')
... 
>>> dir(Foo)
['_Foo__private', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

Notice the _Foo__private method name. You can just call that name.

>>> Foo()._Foo__private()
secret

Things get weird with inheritance. Notice below that the subclass has two private methods. Subclasses need to be aware of the munging not to be surprised. If you use a private method you may find you mess up anyone who inherits from the class and still wants to work with your code.

>>> class Bar(Foo):
...     def __private(self):
...         print('secret 2')
... 
>>> dir(Bar)
['_Bar__private', '_Foo__private', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

Private methods are not part of an implementor's public interface and may be changed at any time without violating the public interface. You can use them, but be aware that all hack rules apply.

tdelaney
  • 73,364
  • 6
  • 83
  • 116