Message399549
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. |
|
Date |
User |
Action |
Args |
2021-08-13 17:47:17 | andrei.avk | set | recipients:
+ 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:17 | andrei.avk | set | messageid: <1628876837.63.0.764588378029.issue35105@roundup.psfhosted.org> |
2021-08-13 17:47:17 | andrei.avk | link | issue35105 messages |
2021-08-13 17:47:17 | andrei.avk | create | |
|