New-style classes and special methods

Raj B rajb at rice.edu
Wed May 30 10:13:47 EDT 2007


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__(...):
		<body>

class B:
	__metaclass__ = C
	<stuff>

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





More information about the Python-list mailing list