New-style classes and special methods

2007-05-30 Thread Raj B
Hi

My question is about how special methods are stored internally in  
Python objects.
Consider a new-style class which implements special methods such as  
__call__ and __new__

class C(type):
def __call__(...):


class B:
__metaclass__ = C


b= B()

The type of C is 'type', that of B is 'C'. When B is instantiated,  
the __call__ method of C is first invoked, since C is the metaclass  
for B.

Internally, when a Python callable object 'obj' is called, the actual  
function called seems to be
'obj->ob_type->tp_call'.

Does this that somehow the '__call__' method defined in C above is  
assigned to the 'tp_call' slot in the object representing the class  
C, instead of it just being stored in the dictionary like a normal  
attribute? Where and how does this magic happen exactly? I'd  
appreciate any level of detail.

Thanks!
Raj


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: New-style classes and special methods

2007-05-30 Thread Raj B
 > 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





-- 
http://mail.python.org/mailman/listinfo/python-list


Representation of new-style instance

2007-08-01 Thread Raj B
Consider a new-style class

class rabbit(object):
   def __init__(self,c):
self.color = c

r1=rabbit("blue")
r2=rabbit("purple")

Which C struct in the Python implementation is used to represent the  
instances c1 and c2 of the
new-style class? I understand that when the class 'rabbit' is  
created, the type_new function
in typeobject.c creates a copy of a 'struct typeobject' with  
dictionary tp_dict appropriately modified.

However, I can't figure out which structure is used for new-style  
instances and where the instance dictionary is stored. Could anyone  
please clarify?

Thanks
Raj
-- 
http://mail.python.org/mailman/listinfo/python-list