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.

Author andrei.avk
Recipients Windson Yang, andrei.avk, chris.jerdonek, docs@python, nedbat, orlnub123, pablogsal, rhettinger, roysmith, serhiy.storchaka, steven.daprano, terry.reedy, xtreak
Date 2021-08-13.17:47:17
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1628876837.63.0.764588378029.issue35105@roundup.psfhosted.org>
In-reply-to
Content
It seems like the documentation is lacking and perhaps misleading in regard to attributes.

- anything hashable can be used as a key in an obj.__dict__: an int, a tuple, etc. According to __dict__ docs, all of those are attributes. There's no warning that this isn't recommended. Various tooling may break, for example dir() will break if keys are not comparable. inspect.getmembers() will break because it relies on dir(). Probably other tooling will break in some way -- this is just the two first things I tried.

Is the reason for allowing that, - only performance (obviously that's a strong enough reason in this case)? Or can this be useful in some other corner cases?

- setattr() requires a string but a string can be '1 2', '(1,2)', etc. The docs for setattr strongly imply that it's the same as dotted notation, but it's not. The non-identifier string attrs don't break dir() or anything in inspect module AFAICT. Should the docs discourage this usage? Should they suggest some use cases where this is useful?

In addition to just being confusing, I think this can create an impression for users that setattr() allows you to set 'private' or 'hidden' attrs, and setting attrs via __dict__ allows you to set even more 'private', 'top secret' attrs.

Since attributes are such a core concept in Python, it might be good to have a section that lays out all of these corner cases and reasons for them, so that it can be linked from docs for setattr(), __dict__, dir(), inspect.getmembers(), etc.
History
Date User Action Args
2021-08-13 17:47:17andrei.avksetrecipients: + andrei.avk, rhettinger, terry.reedy, roysmith, nedbat, steven.daprano, chris.jerdonek, docs@python, serhiy.storchaka, pablogsal, Windson Yang, xtreak, orlnub123
2021-08-13 17:47:17andrei.avksetmessageid: <1628876837.63.0.764588378029.issue35105@roundup.psfhosted.org>
2021-08-13 17:47:17andrei.avklinkissue35105 messages
2021-08-13 17:47:17andrei.avkcreate