Is it possible to make a user defined type be a virtual subclass of a built in type in python? I would like my class to be considered a subclass of int
, however I don't want to inherit directly like this:
class MyInt(int):
'''Do some stuff kind of like an int, but not exactly'''
pass
Since then my class becomes effectively immutable, whether I want it to be or not. For instance, it becomes impossible to use methods like __iadd__
and __isub__
since int
has no way to modify itself. I could inherit from numbers.Integral
, but then when someone calls isinstance(myIntObj, int)
or issubclass(MyInt, int)
the answer will be False
. I understand that classes with a metaclass of ABCMeta can use the method register
to register classes as virtual baseclasses that don't truly inherit from them. Is there some way to do this with built in types? Something like:
registerAsParent(int, MyInt)
I have looked around (both in the python documentation and generally online) and haven't yet found anything close to what I am looking for. Is what I am asking for just completely impossible?