Andrei Kulakov <andrei....@gmail.com> added the comment:

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.

----------
nosy: +andrei.avk

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue35105>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to