[Python-ideas] Missing Core Feature: + - * / | & do not call __getattr__
Brendan Barnwell
brenbarn at brenbarn.net
Fri Dec 4 14:04:46 EST 2015
On 2015-12-04 06:14, Nick Coghlan wrote:
> On 4 December 2015 at 23:55, Stephan Sahm <Stephan.Sahm at gmx.de> wrote:
>> Thanks for the constructive response!
>>
>> this is good to know, however it still forces me make such a dynamic
>> placeholder explicit for every single operator.
>> Still, I could put everything in a Mixin class so that it is in fact useful
>> to clean up code.
>>
>> However, in the long run, such placeholders seem like an unnecessary
>> overhead to me.
>
> The current behaviour is by design - special methods are looked up as
> slots on the object's class, not as instance attributes. This allows
> the interpreter to bypass several steps in the normal instance
> attribute lookup process.
It is worth noting that the behavior is even more magical than this.
Even when looked up on the class, implicit special method lookup
bypasses __getattr__ and __getattribute__ of the metaclass. So the
special method lookup is not just an ordinary lookup that happens to
start on the class instead of the instance; it is a fully magic lookup
that does not engage the usual attribute-access-customization hooks at
any level.
This is
(https://docs.python.org/3/reference/datamodel.html#special-method-lookup)
documented but it is often surprising to new users. Personally I find
it an annoying inconsistency and hope that at some time in the future
Python will become fast enough overall that the extra overhead will be
acceptable.
--
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is no
path, and leave a trail."
--author unknown
More information about the Python-ideas
mailing list