是否有可能成为内置类型的虚拟子类? [英] Is it possible to be a virtual subclass of a built in type?

查看:32
本文介绍了是否有可能成为内置类型的虚拟子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用户定义的类型成为 Python 中内置类型的虚拟子类?我希望我的类被视为 int 的子类,但是我不想像这样直接继承:

class MyInt(int):'''做一些类似于 int 的事情,但不完全是'''经过

从那时起,无论我是否愿意,我的班级都变得有效不可变.例如,不可能使用像 __iadd____isub__ 这样的方法,因为 int 无法修改自身.我可以从 numbers.Integral 继承,但是当有人调用 isinstance(myIntObj, int)issubclass(MyInt, int) 时,答案会是 False.我知道具有 ABCMeta 元类的类可以使用方法 register 将类注册为不真正从它们继承的虚拟基类.有没有办法用内置类型来做到这一点?类似的东西:

registerAsParent(int, MyInt)

我环顾四周(在 python 文档中和一般在线),还没有找到任何接近我正在寻找的东西.我所要求的完全不可能吗?

解决方案

不确定您到底要做什么,因为您要问的是不可能的,因为原始类型本质上是不可变的.但是,您可以覆盖 __iadd__ 等以使用您想要的类型返回结果.请注意,我为戏剧反转了符号(使用 - 而不是 +).

<预><代码>>>>类 MyInt(int):... def __iadd__(self, other):...返回 MyInt(self - other)... def __add__(self, other):...返回 MyInt(self - other)...>>>我 = MyInt(4)>>>我 += 1>>>类型(i)<class '__main__.MyInt'>>>>一世3>>>我 + 5-2>>>类型(i + 5)<class '__main__.MyInt'>

冲洗并重复其余的魔术方法,无论如何您都需要这样做才能拥有 int 的正确"子类(即使虚拟"用户可能希望它们以某种方式运行).

哦,是的,为了可扩展性(好像这还不算疯狂)使用 self.__class__ 代替结果

class MyInt(int):def __iadd__(self, other):return self.__class__(self - other)

所以如果我们有另一个子类.

<预><代码>>>>类 MyOtherInt(MyInt):... def __iadd__(self, other):...返回 self.__class__(self + other)...>>>我 = MyOtherInt(4)>>>我 += 4>>>一世8>>>类型(i)<class '__main__.MyOtherInt'>

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?

解决方案

Not sure what exactly what you are trying to do, as what you are asking is impossible as primitive types are essentially immutable. However you can override __iadd__ and such to return the result with the type you want. Note that I reversed the signs (used - instead of +) for drama.

>>> class MyInt(int):
...     def __iadd__(self, other):
...         return MyInt(self - other)
...     def __add__(self, other):
...         return MyInt(self - other)
... 
>>> i = MyInt(4)
>>> i += 1
>>> type(i)
<class '__main__.MyInt'>
>>> i
3
>>> i + 5
-2
>>> type(i + 5)
<class '__main__.MyInt'>

Rinse and repeat for the rest of the magic methods, which you would have need to done anyway to have a "proper" subclass of int (even if "virtual" users might expect them to function a certain way).

Oh, yeah, for extensibility (as if this wasn't insane already) use self.__class__ instead for the results

class MyInt(int):
    def __iadd__(self, other):
        return self.__class__(self - other)

So if we have another subclass of that.

>>> class MyOtherInt(MyInt):
...     def __iadd__(self, other):
...         return self.__class__(self + other)
... 
>>> i = MyOtherInt(4)
>>> i += 4
>>> i
8
>>> type(i)
<class '__main__.MyOtherInt'>

这篇关于是否有可能成为内置类型的虚拟子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆