[Python-Dev] special method lookup: how much do we care?

Terry Reedy tjreedy at udel.edu
Sat May 9 02:56:25 CEST 2009


Benjamin Peterson wrote:
> 2009/5/8 Terry Reedy <tjreedy at udel.edu>:
>> 2. I am puzzled why those two methods should be extra special, but don't
>> know enough to say more.
> 
> They're not supposed to be special, which is the reason for this
> message. :) Currently the interpreter will call __getattr__ when
> looking them up. This is not the way it should be.

I was trying to ask the same question as Daniel did more clearly, and 
which you answered: they are special special methods because they are 
not in the PyTypeObject struct like the other special (name) methods. 
And that, I presume, is because they are specific to context manager 
objects, while all other 'special' methods (that I notice in 'Special 
method names') are more general in being applicable to multiple types.

Since built-in functions are compiled to load_global, call_function and 
operations to various special op codes, I could imagine that .__enter__ 
and .__exit__ are currently the only implicitly invoked special names 
that explicitly appear in code objects. I can see why you ask before 
burning an opcode (with parameter) to avoid that.

There are two issues: 1) bypass instance lookup; 2) bypass 
.__getattribute__() calling.  I presume you have or can do at least the 
first with a custom .__getattribute__ method.

Terry Jan Reedy







More information about the Python-Dev mailing list