The Voidspace Techie Blog

Gravatar Because there is nothing in Python that isn't an instance of object. For extra marks, explain this: functions, types and classes are all callable, so why don't classes have a __call__ method?

Technically the instances of old-style classes are of type types.InstanceType, so in terms of the class hierarchy based on object they aren't really "instances" of their parent at all!

Old-style classes have a lot of hard-wiring to make them work in the new environment, and in classobject.c the PyClass_Type structure hard-wires the __call__ slot of type.ClassType to PyInstance_New. Being a C function this can't be called from Python.

Pay no attention to the man behind the curtain ...


Gravatar Of course functions, types and classes do have a __call__() method. It is defined in the same place other methods are - in their type:

In [9]: class Bar(object): pass
...:

In [10]: type(Bar).__call__
Out[10]: slot wrapper '__call__' of 'type' objects


Gravatar Cool. Thanks for the explanation Steve.


Gravatar > Because there is nothing in Python that isn't an instance of object.

This has an air of plausibility to it, but consider this (admittedly, frighteningly obscure) code:

>>> class C:
... class __metaclass__(type):
... def mro(self): return []
...
>>> class D(object):
... pass
...
>>> d = D()
>>> d.__class__ = C
>>> isinstance(d, object)
False

(you can probably guess the indentation...)


Name:

Email:

URL:

Comment:  ? 

 

Commenting by HaloScan