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
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: catalin.iacob, ethan.furman, georg.brandl, kj, ncw, r.david.murray
Priority: normal Keywords: needs review, patch

Created on 2010-03-30 10:52 by ncw, last changed 2021-02-13 04:08 by kj.

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 merged kj, 2020-11-25 15:02
Messages (5)
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(hasattr(self, "__private"))
        print(getattr(self, "__private"))

t = Test()


>>> t.test()
Traceback (most recent call last):
  File "", line 10, in <module>
  File "", 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

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  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.
msg386171 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2021-02-02 21:07
New changeset 2edaf6a4fb7e20324dde1423232f07211347f092 by Ken Jin in branch 'master':
bpo-8264: Document hasattr and getattr behavior for private attributes (GH-23513)
msg386900 - (view) Author: Ken Jin (kj) * (Python triager) Date: 2021-02-13 04:08
Thanks for merging this Ethan! I think we can close this now (unless you want me to backport this to 3.9 and 3.8 as well).
Date User Action Args
2021-02-13 04:08:15kjsetmessages: + msg386900
2021-02-02 21:07:07ethan.furmansetnosy: + ethan.furman
messages: + msg386171
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