简体   繁体   中英

Calling a private class member function in python from another module

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 ?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM