合作多重继承问题 [英] Cooperative multiple inheritance issue

查看:113
本文介绍了合作多重继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对这个问题的扩展并提出一个问题,你,我的同伴StackOverflowers,希望能够帮助我。从引用的问题中,考虑最终的代码示例:

This is an extension to this question and raises a problem, which you, my fellow StackOverflowers, will hopefully be able to help me with. From the referenced question, consider the final code sample:

class A(object):
    def __init__(self):
        print "entering A"
        print "leaving A"

class B(object):
    def __init__(self):
        print "entering B"
        super(B, self).__init__()
        print "leaving B"

class C(A,B):
    def __init__(self):
        print "entering c"
        super(C, self).__init__()
        print "leaving c"

正如海报所述,初始化C时,从不调用B的 __ init __ 。在考虑 Raymond Hettinger的帖子时,代码为<$应修改c $ c> A类以调用 super().__ init __()

As noted by the poster, when initialising C, the __init__ for B is never called. On considering Raymond Hettinger's post, the code for class A should be modified to also call super().__init__():

class A(object):
    def __init__(self):
        print "entering A"
        super(A, self).__init__()
        print "leaving A"

到目前为止我们都很好。但是,如果我们的类' __ init __ 函数接受参数怎么办?假设所有 __ init __ 函数都接受一个参数,为了保持一致性,我们只需将其称为 foo ,代码现在是:

We're all good so far. However, what if our class' __init__ functions accept arguments? Let's assume that all the __init__ functions accept a single argument and for consistency, we'll simply call it foo, the code is now:

class A(object):
    def __init__(self, foo):
        print "entering A"
        super(A, self).__init__(foo)
        print "leaving A"

class B(object):
    def __init__(self, foo):
        print "entering B"
        super(B, self).__init__(foo)
        print "leaving B"

class C(A,B):
    def __init__(self, foo):
        print "entering c"
        super(C, self).__init__(foo)
        print "leaving c"

这里我们遇到了障碍。在初始化任何类A,B或C时,我们最终使用单个参数调用 object .__ init __ foo ,哪些错误与 TypeError:object .__ init __()不带参数。但是,要删除其中一个 super().__ init __ 函数,将意味着在多重继承的情况下,这些类不再合作。

Here we hit a snag. When initialising any of the classes A, B or C, we eventually call object.__init__ with a single argument, foo, which errors with TypeError: object.__init__() takes no parameters. However, to remove one of the super().__init__ functions would mean the classes are no longer cooperative in the case of multiple inheritance.

毕竟,我的问题是如何解决这个问题?除了没有参数传递给 __ init __ 函数的情况外,似乎多重继承被破坏了。

After all that, my question is how does one get around this problem? It appears that multiple inheritance is broken in anything but cases where no arguments are passed to the __init__ functions.

更新:

评论中的Rob建议删除关键字参数(在Raymond H的帖子中引用)。这实际上在多重继承的情况下非常有效,直到您对代码进行更改。如果您的某个函数不再使用其中一个关键字参数并且在不修改调用函数的情况下停止剥离它,您仍将收到上面提到的TypeError。因此,对于大型项目来说,这似乎是一个脆弱的解决方案。

Rob in the comments suggests stripping keyword arguments (referenced in Raymond H's post). This actually works very well in the case of multiple inheritance until your change you code. If one of your functions no longer uses one of the keyword arguments and ceases to strip it without modifying the calling function, You will still receive the TypeError noted above. As such, this seems like a fragile solution for large projects.

推荐答案

不可否认,这个解决方案可能不是最好的pythonic或理想的,但是为对象创建一个包装类,允许你在继承中传递每个 __ init __ 的参数:

Admittedly, this solution may not be the most pythonic or ideal, but creating a wrapper class for object like so allows you to pass arguments around each __init__ in the inheritance:

class Object(object):
    def __init__(self,*args,**kwargs):
        super(Object,self).__init__()

class A(Object):
    def __init__(self,*args,**kwargs):
        super(A,self).__init__(*args,**kwargs)

class B(Object):
    def __init__(self,*args,**kwargs):
        super(B,self).__init__(*args,**kwargs)

class C(A,B):
    def __init__(self,*args,**kwargs):
        super(C,self).__init__(*args,**kwargs)

这篇关于合作多重继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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