简体   繁体   English

Python继承:将所有参数从基类传递到超类

[英]Python inheritance: pass all arguments from base to super class

I am not quite used to class inheritance in Python yet. 我还不太习惯使用Python进行类继承。 All I want to do is simply pass all arguments from my base class to the super class when it is created: 我要做的只是在创建基类时将所有参数从我的基类传递给超类:

class A:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def do(self):
        c = self.a + self.b
        return B(c=c)

class B(A):
    def __init__(self, c):
        self.c = c

my_A = A(a=1, b=2)
my_B = my_A.do()
print(my_B.c)

This works as expected. 这按预期工作。 However, what I want is to also be able to call the arguments a and b from the x2 instance of the class my_B, so that I can directly write my_B.a for instance. 但是,我还希望能够从类my_B的x2实例中调用参数ab ,以便可以直接编写my_B.a例如。 I know this is done with super() like this: 我知道这是通过super()完成的,如下所示:

class A:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def do(self):
        c = self.a + self.b
        return B(a=self.a, b=self.b, c=c)

class B(A):
    def __init__(self, a, b, c):
        super(B, self).__init__(a=a, b=b)
        self.c = c

my_A = A(a=1, b=2)
my_B = my_A.do()
print(my_B.a)
print(my_B.b)

However, I don't want to explicitly write all arguments of A when I create the instance of B. Is there a way to automatically pass all arguments from class A to class B? 但是,在创建B的实例时,我不想显式地编写A的所有参数。是否有一种方法可以自动将所有参数从A类传递到B类?

Based on your comment, you could do something like this: 根据您的评论,您可以执行以下操作:

class B(A):
    def __init__(self, c, an_a): 
        super(B, self).__init__(an_a.a, an_a.b)
        self.c = c

You may instead prefer to keep your current constructor and add a from_a static method: 您可能更喜欢保留当前的构造函数并添加from_a静态方法:

class B(A):
    def __init__(self, c, a, b):  # note order
        super(B, self).__init__(a=a, b=b)
        self.c = c

    @staticmethod
    def from_a(c, an_a):
        return B(c, an_a.a, an_a.b)

Finally, if you don't want to type out all of those parameters, you can add an args() method to A and then use the collection unpacking function syntax: 最后,如果您不想键入所有这些参数,则可以向A添加args()方法,然后使用集合拆包函数语法:

class A:
    ...
    def args(self):
        return (self.a, self.b)

class B(A):
    def __init__(self, c, *args):        # Note *args
        super(B, self).__init__(*args)
        self.c = c

    @staticmethod
    def from_a(c, an_a):
        return B(c, *an_a.args())

Now B 's constructor takes the parameter special to B , followed by any number of parameters which just get passed to A 's constructor. 现在, B的构造函数将参数B特殊参数,紧随其后的是传递给A的构造函数的任意数量的参数。 This allows you to do the tuple unpacking when calling the constructor, instead of listing everything out manually. 这使您可以在调用构造函数时进行元组拆包,而不必手动列出所有内容。

Ok, thanks for your comments. 好的,谢谢您的评论。 I have come up with this solution: 我想出了这个解决方案:

class A:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def do(self):
        c = self.a + self.b
        return B(self, c)

class B:
    def __init__(self, base, c):
        self.base = base
        self.c = c

my_A = A(a=1, b=2)
my_B = my_A.do()
print(my_B.base.a)
print(my_B.base.b)
print(my_B.c)

This removes the inheritance of class B and makes the code slightly less readable, but I guess it will do, right? 这消除了类B的继承,并使代码的可读性稍差,但是我想它会这样做,对吗? 😊 😊

yup there is a way use key word arguments so : 是的,有一种使用关键字参数的方法,因此:

class A(object):
    def __init__(self,**kwargs):
        # Non pythonic and a bit of a hack
        self.kwargs = kwargs
        vars(self).update(kwargs)

    def do(self):
        c = self.a + self.b
        return B(c=c, **kwargs)

class B(A):
    def __init__(self, c, **kwargs):
        self.c = c
        super(B, self).__init__(**kwargs)

my_A = A(a=1, b=2)
my_B = my_A.do()
print(my_B.a)
print(my_B.b)
print(my_B.c)

This does what you are after nonethless the way in which it was written before was a bit more pythonic when run this should output: 这会按照您之前编写的方式完成您的工作,然后在运行时输出以下内容:

1
2
3

The downside of doing this is that now A has not limit in terms of the number of attributes but you could ensure this with an assertion or something I guess. 这样做的缺点是,现在A在属性数量方面没有限制,但是您可以通过断言或我猜得到的东西来确保这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM