New-style classes and special methods

Raj B rajb at rice.edu
Wed May 30 11:55:42 EDT 2007


 > Yes, special methods populate the slots in the structures which  
Python
 > uses to represent types.  Objects/typeobject.c in the Python source
 > distribution does the hard work, particularly in function type_new



Thanks for that quick response. I am quite comfortable with C code  
and am trying to understand exactly what happens when a new-style  
class is created, and then instantiated.

I have been reading typeobject.c and type_new() inside it in detail,  
and there are a few issues I am trying to figure out.

I can see a lot of *SLOT() macros in the file that seem to set the  
slots to appropriate values. What I am having trouble figuring out is  
the connection i.e. at what point during construction of the class  
object in type_new() are those slots allotted? Is it the tp_alloc()  
function which does this?

Is there some kind of descriptor or other mechanism connecting  
special method names with their slots in the object representation?  
(e.g. "__call__" with type->tp_call)

Also, what happens when a class inherits from multiple classes with  
their own __call__ methods? Where and how  is it decided which  
__call__ goes into the tp_call slot?

I'm sure I'll eventually figure it out if I stare at the code hard  
enough, but would totally appreciate any help I can get :)

Thanks again!

Raj








More information about the Python-list mailing list