Python 使用不同数量的参数定义init()方法的正确方法(用于多重继承)

Python 使用不同数量的参数定义init()方法的正确方法(用于多重继承),python,Python,这是我当前的设置: class Base(): def __init__(self): pass # ...other methods class A(Base): def __init__(self, dst, fname, alg, src=None): super().__init__() # I then use these instance variables throughout instance methods

这是我当前的设置:

class Base():
    def __init__(self):
        pass

    # ...other methods

class A(Base):
    def __init__(self, dst, fname, alg, src=None):
       super().__init__()
       # I then use these instance variables throughout instance methods.
       self.src = [] if src is None else src
       self.dst = dst
       self.fname = fname
       self.alg = alg

    # ...other methods

class B(Base):
    def __init__(self, fname):
       super().__init__()
       # I then use these instance variables throughout instance methods.
       self.fname = fname

    # ...other methods

class C(A, B):
    """Here is my problem.
       When I try to inherit A and B this way,
       I keep getting those "missing required positional args..." errors
    """
    def __init__(self, dst, src=None):
       super().__init__()
       # I then use these instance variables throughout instance methods.
       self.fname = fname

    # ...other methods
class Base():
    def __init__(self, *args, **kwargs):
        pass

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

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

class C(A, B):
    def __init__(self, *args, **kwargs):
       super().__init__(*args, **kwargs)
以下是我现在正在尝试做的事情:

class Base():
    def __init__(self):
        pass

    # ...other methods

class A(Base):
    def __init__(self, dst, fname, alg, src=None):
       super().__init__()
       # I then use these instance variables throughout instance methods.
       self.src = [] if src is None else src
       self.dst = dst
       self.fname = fname
       self.alg = alg

    # ...other methods

class B(Base):
    def __init__(self, fname):
       super().__init__()
       # I then use these instance variables throughout instance methods.
       self.fname = fname

    # ...other methods

class C(A, B):
    """Here is my problem.
       When I try to inherit A and B this way,
       I keep getting those "missing required positional args..." errors
    """
    def __init__(self, dst, src=None):
       super().__init__()
       # I then use these instance variables throughout instance methods.
       self.fname = fname

    # ...other methods
class Base():
    def __init__(self, *args, **kwargs):
        pass

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

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

class C(A, B):
    def __init__(self, *args, **kwargs):
       super().__init__(*args, **kwargs)
我的主要问题是:

处理此类情况的“最佳”(标准、首选、高效、可读等)方式是什么

附言

  • 我读了下面这篇文章,但读完后,我无法为自己找到最好的答案
  • 此外,我引用了,但接受的答案没有像我一样有不同数量的参数
  • 顺便说一句,我愿意听到我的设计(类层次结构)总体上是糟糕的。从技术上讲,我可以将我试图从C中的A和B继承的方法重新定位到基类……我希望(还没有尝试过)

基于您的实现,为了消除错误,我认为最好基于显式使用基类的必需参数(在您的情况下,
类C
的基类应该是
类A


这将是可以做到的最简单的解决方案。但是,如果您愿意进行设计更改,我建议对python使用组合而不是继承。我假设您在
类A
类B
中有
类C
所需的方法。使用,只需将所需的函数/操作添加到需要它的类中即可。它还可以防止代码闻起来像Python具有支持多重继承的优势,甚至票房技术java也不支持此功能,因为当两个扩展类具有相同的方法名时,它会导致重写方法

但是python克服了这个问题并支持多重继承。要在python中使用多重继承,应该知道要以正确的方式定义的参数

class C(A, B):
    def __init__(self, dst, src=None):
       super().__init__()
       self.fname = fname
在上面的obove代码中,类C继承类A和B。这里首先是super()。\uuuu init\uuu()导航到类A,搜索。\uuu init\uuu(),如果init()或其他方法abc()不在类A中,那么接下来它将查找类B

这些导航是基于继承到类C中的顺序进行的


这个问题的解决方案是,您使用类A和B中的参数定义了超类\uuu init\uuuuuuuuu(),并调用了超类super()。\uuu init\uuuuuuuuu()而不使用类C中的参数。

如果
A
B
需要这些不同的签名,并且需要位置参数,您是否应该同时从
A
B
继承
C
?你希望这个类的用户如何理解它?在什么样的例子中,这是有意义的?@Grismar我想这是一个很好的观点……就像我提到的,也许我的逻辑没有意义。我之所以想从A和B继承,是因为它们都有一些我想在C中使用的方法。我应该把这些常用方法移到基类中吗?你能分享一下你是如何使用类的吗?