Changing return of type(obj)

Pekka Klärck peke at iki.fi
Sun Feb 8 08:20:00 EST 2009


2009/2/6 Ken Elkabany <Ken at elkabany.com>:
>
> I am attempting to fully-simulate an 'int' object with a custom object type.
> It is part of a library I am creating for python futures and promises. Is
> there anyway such that type(my_object) can return <type 'int'>?

If it's enough to change how the type looks like you can simply change its repr:

>>> class X(type):
...   def __repr__(self):
...     return 'xxx'
...
>>> class Y(object):
...   __metaclass__ = X
...
>>> type(Y())
xxx


Cheers,
    .peke



More information about the Python-list mailing list