[Python-ideas] Eliminating special method lookup (was Re: Missing Core Feature: + - * / | & do not call __getattr__)

Brendan Barnwell brenbarn at brenbarn.net
Sat Dec 5 00:50:58 EST 2015


On 2015-12-04 18:38, Steven D'Aprano wrote:
> Well, maybe. I haven't decided whether special method lookup is an
> annoying inconsistency or a feature. It seems to me that operators, at
> least, are somewhat different from method calls, and the special
> handling should be considered a deliberate design feature. My
> reasoning goes like this:

	What you're describing is orthogonal to the issue I was raising.  I 
agree that it makes sense for "obj1 + obj2" to look up __add__ on the 
class.  What I'm saying is that currently the lookup of __add__ on the 
class is *still* not an ordinary lookup.  An ordinary lookup on the 
class would make use of __getattr__/__getattribute__ on the *metaclass* 
(just as an ordinary lookup on the instance, like foo.attr, would make 
use of __getattr__ on the class).  But special method lookup doesn't do 
this; type(foo).__getattribute__("__add__") is never called.  There is 
no way to customize special method name lookup at all.  You have to 
actually define the methods on the class.  (You can do that in 
labor-saving ways like writing a metaclass or class decorator that 
inserts appropriate methods into the class dict, but you still have to 
statically set those methods; you can't intercept each special method 
lookup as it comes in and decide what to do with it, as you can with 
other attributes.)

-- 
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