|
|
|
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 ...
Steve Holden |
Homepage |
07/01/26 - 10:14 am | #
|
|
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
Kent Johnson |
Homepage |
07/01/26 - 12:51 pm | #
|
|
Cool. Thanks for the explanation Steve.
Fuzzyman |
Homepage |
07/01/26 - 2:49 pm | #
|
|
> 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...)
Michael Hudson |
Homepage |
07/01/29 - 2:33 pm | #
|
|
|
Commenting by HaloScan
|