New submission from Nick Craig-Wood <[email protected]>:
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.
----------
components: Interpreter Core
messages: 101928
nosy: ncw
severity: normal
status: open
title: hasattr doensn't show private (double underscore) attributes exist
type: behavior
versions: Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.3
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue8264>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com