[英]when is super(Baseclass, self).__init__ used
When should the following code be used in Python (Assume that Baseclass inherits from Parent class and Parent class has some variables initiated in __init__ () method) 何时应在Python中使用以下代码(假定Baseclass继承自Parent类,并且Parent类在__init__ ()方法中初始化了一些变量)
class Baseclass(Parent):
def __init__(self, some_arg):
self.some_arg = some_arg
super(Baseclass, self).__init__()
Does this code makes all the local variables defined in __init__ method of Parent class accessible in Baseclass? 此代码是否可以在Baseclass中访问在Parent类的__init__方法中定义的所有局部变量? What significance does it make?
它有什么意义?
super
keeps your code from being repetitive; super
防止您的代码重复出现; a complex __init__
needn't be c/p'ed into your inheriting classes. 不需要将复杂的
__init__
放入继承的类中。 It also makes MRO work as it should, such that if you use multiple inheritance it will work correctly. 它还可以使MRO正常工作,因此,如果使用多重继承,它将可以正常工作。
One reason to do this would be to ensure that all of your inheriting objects have certain attributes which they don't have from the parent. 这样做的原因之一是要确保所有继承对象都具有某些父级不具备的属性。 If you simply write a new
__init__
, they won't have them unless you repeat your code. 如果您只是简单地编写一个新的
__init__
,则除非重复您的代码,否则它们将没有它们。 For example: 例如:
>>> class A(object):
... def __init__(self, x):
... self.x = x
...
>>> class B(A):
... def __init__(self, y):
... self.y = y
...
>>> Stick = B(15)
>>> Stick.x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'B' object has no attribute 'x'
>>>
Without calling super
during the __init__
the entire method is simply overridden. 无需在
__init__
期间调用super
,整个方法将被简单地覆盖。 A call to super
here ensures that both variables exist in the inherited class. 此处对
super
的调用可确保两个变量都存在于继承的类中。
>>> class C(A):
... def __init__(self, x, y):
... super(C, self).__init__(x)
... self.y = y
...
>>> Dave = C(15, 22)
>>> Dave.x
15
>>> Dave.y
22
>>>
Note that in the super
call, x
is passed to the __init__()
call, but self
is taken care of in the super(C, self)
part of the code. 请注意,在
super
调用中, x
传递给__init__()
调用,但是self
在代码的super(C, self)
部分中得到照顾。
EDIT: TyrantWave also rightly points out that super
is also quite useful outside of __init__
. 编辑:TyrantWave也正确地指出,在
__init__
之外, super
也非常有用。 Take an object with a simple foo
method for example. 以带有简单
foo
方法的对象为例。
class Parent(object):
def foo(self):
return "I say foo!"
The inherited class may want to just alter the output of this function instead of totally rewriting it. 继承的类可能只想更改此函数的输出,而不是完全重写它。 So instead of repeating ourselves and writing the same code over again, we just call
super
to get the parent's return value, then work with the data and return the child class's modified results. 因此,我们无需重复自己并再次编写相同的代码,而只需调用
super
以获取父级的返回值,然后使用该数据并返回子级的修改后的结果。
class Child(Parent):
def foo(self):
parent_result = super(Child, self).foo()
return "I'm a child!! %s" % parent_result
In the above, the call to super
returns the Parent
s value for foo()
and then the Child
goes on to work with the data further before returning it themselves. 在上面的代码中,对
super
的调用返回foo()
的Parent
的值,然后Child
继续进一步处理数据,然后再返回自身。
>>> Alan = Parent()
>>> Stan = Child()
>>> Alan.foo()
'I say foo!'
>>> Stan.foo()
"I'm a child!! I say foo!"
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.