Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doc] hasattr doesn't show private (double underscore) attributes exist #52511

Closed
ncw mannequin opened this issue Mar 30, 2010 · 7 comments
Closed

[doc] hasattr doesn't show private (double underscore) attributes exist #52511

ncw mannequin opened this issue Mar 30, 2010 · 7 comments
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error

Comments

@ncw
Copy link
Mannequin

ncw mannequin commented Mar 30, 2010

BPO 8264
Nosy @birkenfeld, @bitdancer, @ethanfurman, @miss-islington, @iritkatriel, @Fidget-Spinner
PRs
  • bpo-8264: Document hasattr and getattr behavior for private attributes #23513
  • [3.9] bpo-8264: Document hasattr and getattr behavior for private attributes (GH-23513) #26238
  • Files
  • get-set-attr-private-name-mangling.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-05-22.13:00:13.650>
    created_at = <Date 2010-03-30.10:52:24.890>
    labels = ['type-bug', '3.8', '3.9', '3.10', 'docs']
    title = "[doc] hasattr doesn't show private (double underscore) attributes exist"
    updated_at = <Date 2021-05-22.13:00:13.650>
    user = 'https://bugs.python.org/ncw'

    bugs.python.org fields:

    activity = <Date 2021-05-22.13:00:13.650>
    actor = 'kj'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2021-05-22.13:00:13.650>
    closer = 'kj'
    components = ['Documentation']
    creation = <Date 2010-03-30.10:52:24.890>
    creator = 'ncw'
    dependencies = []
    files = ['26289']
    hgrepos = []
    issue_num = 8264
    keywords = ['patch', 'needs review']
    message_count = 7.0
    messages = ['101928', '101932', '164820', '386171', '386900', '394182', '394183']
    nosy_count = 8.0
    nosy_names = ['georg.brandl', 'ncw', 'r.david.murray', 'ethan.furman', 'catalin.iacob', 'miss-islington', 'iritkatriel', 'kj']
    pr_nums = ['23513', '26238']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue8264'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    @ncw
    Copy link
    Mannequin Author

    ncw mannequin commented Mar 30, 2010

    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.

    @ncw ncw mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Mar 30, 2010
    @bitdancer
    Copy link
    Member

    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.)

    @bitdancer bitdancer added docs Documentation in the Doc dir and removed interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Mar 30, 2010
    @admin admin mannequin assigned docspython and unassigned birkenfeld Oct 29, 2010
    @cataliniacob
    Copy link
    Mannequin

    cataliniacob mannequin commented Jul 7, 2012

    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.

    @cataliniacob cataliniacob mannequin changed the title hasattr doensn't show private (double underscore) attributes exist hasattr doesn't show private (double underscore) attributes exist Jul 7, 2012
    @iritkatriel iritkatriel added 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes labels Nov 25, 2020
    @iritkatriel iritkatriel changed the title hasattr doesn't show private (double underscore) attributes exist [doc] hasattr doesn't show private (double underscore) attributes exist Nov 25, 2020
    @ethanfurman
    Copy link
    Member

    New changeset 2edaf6a by Ken Jin in branch 'master':
    bpo-8264: Document hasattr and getattr behavior for private attributes (GH-23513)
    2edaf6a

    @Fidget-Spinner
    Copy link
    Member

    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).

    @iritkatriel
    Copy link
    Member

    New changeset 11b5045 by Miss Islington (bot) in branch '3.9':
    bpo-8264: Document hasattr and getattr behavior for private attributes (GH-23513) (GH-26238)
    11b5045

    @Fidget-Spinner
    Copy link
    Member

    All the PRs have landed in bugfix branches so I'm closing this issue. Thanks Irit and Ethan!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes 3.9 only security fixes 3.10 only security fixes docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants