Preventing execution of a method

alex23 wuwei23 at gmail.com
Thu Dec 11 18:22:21 CET 2008


On Dec 12, 2:35 am, rdmur... at bitdance.com wrote:
> There is, however, also the possibility of prefixing the method name
> with '__'.  The invokes 'name mangling', which makes it more difficult
> (though not impossible, the idea is to avoid accidents) for the method
> to be called from outside the class.

That only works for methods, it has no effect on functions or classes
within modules:

module.py:
    def __f(): pass

    class __F(object):
        def __f(): pass

>>> import module as m
>>> m.__f
<function __f at 0x00B80FB0>
>>> m.__F
<class 'module.__F'>
>>> f = m.__F()
>>> f.__f
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: '__F' object has no attribute '__f'
>>> f._F__f
<bound method __F.__f of <module.__F object at 0x00B89B30>>



More information about the Python-list mailing list