Type of an object:

Gregory Ewing greg.ewing at canterbury.ac.nz
Wed Dec 18 12:45:02 CET 2013


Ethan Furman wrote:
>> This leads to another question: we've now seen two examples where
>> (presumably) the internal type field and __class__ differ. In the
>> weakproxy case, type(obj) returns the internal type field. In the
>> "regular" case, where you set obj.__class__ to a class, type(obj) returns
>> the new (external) type. How the hell does it decide which one to return?

When you set the __class__ of a regular object and it
succeeds, it actually changes the internal type, i.e.
obj->ob_type at the C level.

This is only allowed under certain conditions. Mostly
only for user-defined classes, and only if the layouts
of the old and new instance structs are compatible.

-- 
Greg



More information about the Python-list mailing list