Message408133
If you don't want to customise attribute access, don't overload
`__getattribute__`. The documentation for `__getattribute__` is clear
about what it does:
"Called unconditionally to implement attribute accesses for instances of
the class."
https://docs.python.org/3/reference/datamodel.html#object.__getattribute__
That includes lookups for `__class__`, regardless of whether the
function doing the lookup is isinstance or some other function.
You ask:
"Are there any reasons why __class__ cannot be retrieved with
object.__getattribute__ instead?"
Yes, that would ignore overloaded attribute access, which would be a
bug in my opinion.
Being able to dynamically generate computed attributes by overloading
`__getattr__` and `__getattribute__` is not a bug. It is part of
Python's data model. See the documentation above.
And that includes `__class__`.
I don't believe that it is an accident or a bug that isinstance uses
ordinary attribute lookup, which goes through the standard mechanism
including the class' own `__getattribute__` method. But I will ask on
the Python-Dev mailing list in case Guido or other core developers think
that it is wrong to do so. |
|
Date |
User |
Action |
Args |
2021-12-09 15:34:56 | steven.daprano | set | recipients:
+ steven.daprano, r.david.murray, bup, Gabriele Tornetta |
2021-12-09 15:34:56 | steven.daprano | link | issue32683 messages |
2021-12-09 15:34:56 | steven.daprano | create | |
|