private class members and hasattr

Francis Avila francisgavila at yahoo.com
Fri Jan 23 04:37:38 EST 2004


Dragos Chirila wrote in message ...

>Why hasattr method doesn't return 1(true) for private member '__prop3' ??

Because that's how private members work.  See "5.2.1 Identifiers (Names)" in
the Python Language Reference.

>>> class PropTest:
...     def __init__(self):
...         self.__prop3 = 3
...     def testprops(self):
...         print hasattr(self, '__prop3')
...         print hasattr(self, '_PropTest__prop3')
...
>>> dir(PropTest())
['_PropTest__prop3', '__doc__', '__init__', '__module__', 'testprops']
>>> PropTest().testprops()
False
True
>>>

You're probably better off not using private members anyway.
--
Francis Avila




More information about the Python-list mailing list