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.

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

Created on 2010-03-30 10:52 by ncw, last changed 2022-04-11 14:56 by admin. This issue is now closed.

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 merged kj, 2020-11-25 15:02
PR 26238 merged miss-islington, 2021-05-19 09:20
Messages (7)
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.
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)
https://github.com/python/cpython/commit/2edaf6a4fb7e20324dde1423232f07211347f092
msg386900 - (view) Author: Ken Jin (kj) * (Python committer) 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).
msg394182 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-05-22 11:06
New changeset 11b5045b0ce18fee8cc1023cc516aeb14ebf7bda by Miss Islington (bot) in branch '3.9':
bpo-8264: Document hasattr and getattr behavior for private attributes (GH-23513) (GH-26238)
https://github.com/python/cpython/commit/11b5045b0ce18fee8cc1023cc516aeb14ebf7bda
msg394183 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2021-05-22 13:00
All the PRs have landed in bugfix branches so I'm closing this issue. Thanks Irit and Ethan!
History
Date User Action Args
2022-04-11 14:56:59adminsetgithub: 52511
2021-05-22 13:00:13kjsetstatus: open -> closed
resolution: fixed
messages: + msg394183

stage: patch review -> resolved
2021-05-22 11:06:55iritkatrielsetnosy: + iritkatriel
messages: + msg394182
2021-05-19 09:20:06miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request24855
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