__bases__ 不起作用!接下来是什么?

发布于 2024-09-08 03:57:48 字数 309 浏览 7 评论 0原文

以下代码在 Python 3.x 中不起作用,但它曾经适用于旧式类:

class Extender:
    def extension(self):
        print("Some work...")

class Base:
    pass

Base.__bases__ += (Extender,)

Base().extension()

问题很简单: 如何动态(在运行时)添加超类Python 3.x 中的类?

但我已经准备好答案将是困难的! )

The following code doesn't work in Python 3.x, but it used to work with old-style classes:

class Extender:
    def extension(self):
        print("Some work...")

class Base:
    pass

Base.__bases__ += (Extender,)

Base().extension()

Question is simple: How can I add dynamically (at runtime) a super class to a class in Python 3.x?

But I'm ready the answer will be hard! )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话难免假 2024-09-15 03:57:48

看来可以动态更改Base.__bases__
如果Base.__base__不是object。 (动态更改是指从 Base 继承的所有预先存在的实例也会动态更改。否则请参阅 Mykola Kharechko 的解决方案)。

如果 Base.__base__ 是某个虚拟类 TopBase,那么对 Base.__bases__ 的赋值似乎可以工作:经过

class Extender(object):
    def extension(self):
        print("Some work...")

class TopBase(object):
    pass

class Base(TopBase):
    pass

b=Base()
print(Base.__bases__)
# (<class '__main__.TopBase'>,)

Base.__bases__ += (Extender,)
print(Base.__bases__)
# (<class '__main__.TopBase'>, <class '__main__.Extender'>)
Base().extension()
# Some work...
b.extension()
# Some work...

Base.__bases__ = (Extender, TopBase) 
print(Base.__bases__)
# (<class '__main__.Extender'>, <class '__main__.TopBase'>)
Base().extension()
# Some work...
b.extension()
# Some work...

测试,可以在 Python 2 中工作(对于新式和旧式类)和Python 3。我不知道为什么它可以工作,而这却不能:

class Extender(object):
    def extension(self):
        print("Some work...")

class Base(object):
    pass

Base.__bases__ = (Extender, object)
# TypeError: __bases__ assignment: 'Extender' deallocator differs from 'object'

It appears that it is possible to dynamically change Base.__bases__
if Base.__base__ is not object. (By dynamically change, I mean in such a way that all pre-existing instances that inherit from Base also get dynamically changed. Otherwise see Mykola Kharechko's solution).

If Base.__base__ is some dummy class TopBase, then assignment to Base.__bases__ seems to work:

class Extender(object):
    def extension(self):
        print("Some work...")

class TopBase(object):
    pass

class Base(TopBase):
    pass

b=Base()
print(Base.__bases__)
# (<class '__main__.TopBase'>,)

Base.__bases__ += (Extender,)
print(Base.__bases__)
# (<class '__main__.TopBase'>, <class '__main__.Extender'>)
Base().extension()
# Some work...
b.extension()
# Some work...

Base.__bases__ = (Extender, TopBase) 
print(Base.__bases__)
# (<class '__main__.Extender'>, <class '__main__.TopBase'>)
Base().extension()
# Some work...
b.extension()
# Some work...

This was tested to work in Python 2 (for new- and old-style classes) and for Python 3. I have no idea why it works while this does not:

class Extender(object):
    def extension(self):
        print("Some work...")

class Base(object):
    pass

Base.__bases__ = (Extender, object)
# TypeError: __bases__ assignment: 'Extender' deallocator differs from 'object'
浴红衣 2024-09-15 03:57:48

对于我来说这是不可能的。但您可以动态创建新类:

class Extender(object):
    def extension(self):
        print("Some work...")

class Base(object):
    pass

Base = type('Base', (Base, Extender, object), {})
Base().extension()

As for me it is impossible. But you can create new class dynamically:

class Extender(object):
    def extension(self):
        print("Some work...")

class Base(object):
    pass

Base = type('Base', (Base, Extender, object), {})
Base().extension()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文