classification
Title: [doc] hasattr doesn't show private (double underscore) attributes exist
Type: behavior Stage: patch review
Components: Documentation Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: catalin.iacob, georg.brandl, kj, ncw, r.david.murray
Priority: normal Keywords: needs review, patch

Created on 2010-03-30 10:52 by ncw, last changed 2020-11-25 15:02 by kj.

Files
File name Uploaded Description Edit
get-set-attr-private-name-mangling.patch catalin.iacob, 2012-07-07 11:08 review
Pull Requests
URL Status Linked Edit
PR 23513 open kj, 2020-11-25 15:02
Messages (3)
msg101928 - (view) Author: Nick Craig-Wood (ncw) * Date: 2010-03-30 10:52
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.
msg101932 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2010-03-30 11:22
You are correct, this is the expected behavior.  Name mangling happens only at compilation time (see http://docs.python.org/reference/expressions.html#atom-identifiers).  A doc note for get/setattr would probably be useful.

(I removed 3.3; that version doesn't exist yet and should only be set on issues that can only be dealt with in 3.3, such as removing something deprecated in 3.2.)
msg164820 - (view) Author: Catalin Iacob (catalin.iacob) * Date: 2012-07-07 11:08
Attached a patch that adds a note for getattr and setattr. hasattr is documented in terms of getattr so I would say it's not needed there.

I don't know if the interaction with private attributes is confusing enough that it's worth increasing the verbosity of the getattr and setattr docs, all in all I think I'm +0 to adding the note and -0 to just closing the bug as by design and no doc change needed.
History
Date User Action Args
2020-11-25 15:02:05kjsetnosy: + kj
pull_requests: + pull_request22400
2020-11-25 12:56:42iritkatrielsettitle: hasattr doesn't show private (double underscore) attributes exist -> [doc] hasattr doesn't show private (double underscore) attributes exist
versions: + Python 3.8, Python 3.9, Python 3.10, - Python 2.7, Python 3.3, Python 3.4
2014-01-29 07:41:20serhiy.storchakasetkeywords: + needs review
stage: needs patch -> patch review
versions: + Python 3.3, Python 3.4, - Python 2.6, Python 3.1, Python 3.2
2012-07-07 11:08:49catalin.iacobsetfiles: + get-set-attr-private-name-mangling.patch
title: hasattr doensn't show private (double underscore) attributes exist -> hasattr doesn't show private (double underscore) attributes exist
nosy: + catalin.iacob

messages: + msg164820

keywords: + patch
2010-10-29 10:07:21adminsetassignee: georg.brandl -> docs@python
2010-03-30 11:22:36r.david.murraysetpriority: normal

assignee: georg.brandl
components: + Documentation, - Interpreter Core
versions: - Python 3.3
nosy: + georg.brandl, r.david.murray

messages: + msg101932
stage: needs patch
2010-03-30 10:52:24ncwcreate