New-style classes and special methods

Alex Martelli aleax at mac.com
Wed May 30 22:47:30 EDT 2007


Raj B <rajb at rice.edu> wrote:

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

I believe there are different times -- one for the fundamental struct
that represents all types, others for secondary structs that represent
e.g. numerical/arithmetic methods, sequence methods, etc, each of which
is present only if necessary for a given type.

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

The online docs for the C/API interface do some effort at explaining the
type-struct and auxiliary ones, and I spent a couple of pages on that
same subject in "Python in a Nutshell", though nowhere close to doing it
justice (such docs normally address people who want to write C-coded
extensions, rather than ones whose goal is to understand the existing
Python runtime -- though many of the issues are identical).

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

The MRO is used (that stands for Method Resolution Order, and is also
implemented in the same C file, and documented there AND in a strong
essay by M. Simionato whose URL is, I believe, also in a comment in that
file) to find the relevant implementation.


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

You're welcome, but I hope somebody else will also step up to offer
useful comments, because (what between my talk at Google Developer's Day
tomorrow, and traveling next week to speak in Krakow and then at the
first Italian conference on Python in Florence) I'm rather overwhelmed
these days;-).


Alex



More information about the Python-list mailing list