This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author ncw
Recipients ncw
Date 2010-03-30.10:52:24
SpamBayes Score 2.2409297e-12
Marked as misclassified No
Message-id <1269946346.53.0.630682854213.issue8264@psf.upfronthosting.co.za>
In-reply-to
Content
I just spend a while tracking down a bug in my code which turned out to be an unexpected behaviour of hasattr.

Running this

class Test(object):
    def __init__(self):
        self.__private = "Hello"
    def test(self):
        print(self.__private)
        print(hasattr(self, "__private"))
        print(getattr(self, "__private"))


t = Test()
t.test()

Prints

>>> t.test()
Hello
False
Traceback (most recent call last):
  File "private_test.py", line 10, in <module>
    t.test()
  File "private_test.py", line 7, in test
    print(getattr(self, "__private"))
AttributeError: 'Test' object has no attribute '__private'
>>>

Indicating that even though we just printed self.__private hasattr() can't find it nor getattr().

I think this is probably the intended behaviour, but it does seem inconsistent.

Probably all that is required is a documentation patch...

Maybe something add something like this to the end of

  http://docs.python.org/library/functions.html#hasattr

Note that hasattr won't find private (double underscore) attributes unless the mangled name is used.
History
Date User Action Args
2010-03-30 10:52:26ncwsetrecipients: + ncw
2010-03-30 10:52:26ncwsetmessageid: <1269946346.53.0.630682854213.issue8264@psf.upfronthosting.co.za>
2010-03-30 10:52:24ncwlinkissue8264 messages
2010-03-30 10:52:24ncwcreate