Changing return of type(obj)

Christian Heimes lists at cheimes.de
Fri Feb 6 07:05:50 EST 2009


Ken Elkabany schrieb:
> I would simply subclass 'int', but this object needs to be general enough to
> pretend to be an 'int', 'NoneType', 'str', etc... A long shot: Can I change
> the base class on an instance by instance basis depending on the need? Well,
> now I can imagine having a class factory that will spawn for me the class
> that inherits the correct base type. Any other solutions?

No, it's not possible. The type is an inherent part of an object. Python
looks up methods and other attributes by looking at the type. The method
call obj.method() is implemented as type(obj).method(obj).

Christian




More information about the Python-list mailing list