-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Unexpected behavior with __dict__
as a class attribute
#102648
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
Comments
Thanks for the report! This is indeed confusing. The reason for this behavior is that normally classes have a descriptor for the I don't see a clear way to allow this override and "fix" the odd behavior without breaking Python's object model more deeply. We could raise an error on override of the Will leave this open for now to see what others think. |
I thought I remembered reading an issue about this before, but can't immediately find it |
I also searched and didn't find anything, but that gives me very little confidence that prior discussion doesn't exist :) |
Anyway, whether or not we fix/document this, and whether or not we have discussed this before: it doesn't surprise me that this does something strange and unexpected. So "don't do that" is probably good advice here :) |
I agree that we never said that this will work. And me personally would not want this to work. Moreover, >>> class My: ...
...
>>> My.__dict__
mappingproxy({'__module__': '__main__', '__dict__': <attribute '__dict__' of 'My' objects>, '__weakref__': <attribute '__weakref__' of 'My' objects>, '__doc__': None}) It is an explicitly immutable type. So, I think we can just recommend "Do not write this code, please". |
Bug report
When one puts
__dict__
as a class attribute, the behavior becomes very odd:Moving it to be an instance attribute, does what one expects:
I know one shouldn't have a
__dict__
as a class attribute, but I think it would be nice to either have a good error message when that happens, or to make things more consistent, i.e.all([hasattr(a, x) for x in dir(a)])
should be true maybe?We encountered this issue since certain
pickle
related logic was changed in 3.11: scikit-learn/scikit-learn#25188 (comment)Your environment
The text was updated successfully, but these errors were encountered: