Raj B 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?
The place to start is the PyType_Type tp_new slot function type_new(). The second to last statement is a call to fixup_slot_dispatchers(). This function goes through the dictionary looking for special methods and adds the appropriate slot functions. This gets very involved. I had to use the Visual Studio debugger to follow what was happening when trying to figure out what happens when assigning a special method to a class after it is declared. > > 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) There is a special tp_call slot function, slot_tp_call(), that calls the user defined __call__. The same goes for other special methods. Descriptors only come into play with extension types. In this case if a slot function is found a descriptor is added to make the slot function accessible from Python as a special method. > > 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? > As Alex Martelli mentioned, __call__ is found using the method resolution order. The tp_call slot function slot_tp_call() uses lookup_method(), a variation of PyObject_GetAttribute(), to finding the appropriate Python method. Its all documented in the C file. > 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 :) > Just ask. -- Lenard Lindstrom <[EMAIL PROTECTED]> -- http://mail.python.org/mailman/listinfo/python-list