__class__ and type() implementation details in CPython

Ivan Yurchenko ivan0yurchenko at gmail.com
Sun Feb 10 12:35:29 CET 2013


I've done the following in CPython 2.7.3 and 3.3.0 (and also in PyPy 2.0b1):

>>> import weakref
>>> x = set()
>>> y = weakref.proxy(x)
>>> x.__class__, type(x), isinstance(x, set)
(<class 'set'>, <class 'set'>, True)
>>> y.__class__, type(y), isinstance(y, set)
(<class 'set'>, <class 'weakproxy'>, True)

So, type doesn't use object's __class__ to determine its class. I'm looking
for some CPyhton implementation details - how does class identification with
type() work? According to CPython's sources it looks like there is a "marker"
of actual object's class associated with each PyObject - _typeobject struct,
which is used to identify the class by type(). Am I right?

Thank you.

- Ivan Yurchenko.

More information about the Python-list mailing list