理解在子类中调用父 __init__ [英] Understanding calling parent __init__ in subclass
问题描述
Python 新手,很好奇是否有人可以尽可能通俗易懂地解释一下,为什么在创建子类时,仍然需要调用父初始化函数?
New to Python and was curious if someone could explain, in layman's terms as much as possible, why it is that, when you're creating a subclass, you still need to call the parent initialization function?
假设您有一个这样的父类:
Let's say you have a parent class like this:
class Pet(object):
def __init__(self, name, species):
self.name = name
self.species = species
def getName(self):
return self.name
def getSpecies(self):
return self.species
def __str__(self):
return "%s is a %s" % (self.name, self.species)
然后我想创建一个名为Dog"的子类,它显然使用了父类中的一些函数.我的研究告诉我,我需要做这样的事情:
Then I want to make a subclass called "Dog" that obviously uses some of the functions in the parent class. My research is telling me that I need to do something like this:
class Dog(Pet):
def __init__(self, name, chases_cats):
Pet.__init__(self, name, "Dog")
self.chases_cats = chases_cats
def chasesCats(self):
return self.chases_cats
但是为什么,如果我在定义子类时将父类作为参数传递,我是否需要调用父类的 __init__
?这不是因为它是子类而自动传递给子类吗?
But why, if I'm passing the parent class as an argument when defining the subclass, do I need to call the parent's __init__
? Isn't this automatically passed to the subclass by virtue of the fact that it's a subclass?
在我的理解中,我确定我在这里遗漏了一些明显的东西,但它只是不适合我.
I'm sure I'm missing something obvious here in my understanding, but it's just not clicking for me.
推荐答案
我要问的是:为什么它会这样工作?如果你是从父类继承的,那么你继承父类的所有东西,包括
__init__
,难道不合理吗?
如果您要覆盖它,则不会.如果您的子类没有定义自己的 __init__
,那么确实会调用超类 __init__
.但是,如果子类确实定义了自己的 __init__
,那么您就是在覆盖它,因此 Python 不会假设您要如何或是否要调用超类实现.
Not if you're overriding it. If your subclass doesn't define its own __init__
, then the superclass __init__
is indeed called. But if the subclass does define its own __init__
, then you're overriding it, so Python doesn't make asusumptions about how or whether you want to call the superclass implementation.
这为您提供了额外的灵活性.如果超类 __init__
被自动调用,则必须在子类 __init__
之前或之后统一调用它.但由于它不会自动调用,因此您可以随时调用它.你可以在子类 __init__
的开头、结尾、中间的某个地方调用它,或者根本不调用它.这意味着您的子类可以在超类方法运行之前设置"一些东西,或者在超类方法运行之后调整"一些东西,或者两者都做,或者完全替换超类的功能.
This gives you additional flexibility. If the superclass __init__
were called automatically, it would have to be uniformly called before or after the subclass __init__
. But since it's not called automatically, you can call it whenever you like. You can call it at the beginning of the subclass __init__
, at the end, somewhere in the middle, or not at all. This means that your subclass can either "set up" things before the superclass method acts, or "tweak" things after the superclass method acts, or do both, or completely replace the superclass functionality.
此外,如果自动调用超类实现,如果子类不接受与超类相同的参数,将不清楚该怎么做.通过显式超类调用,您的子类可以接受比其超类更多或更少的参数,并且可以自行决定在调用超类时传递什么.
Also, if the superclass implementation were called automatically, it would be unclear what to do if the subclass didn't accept the same arguments as the superclass. With explicit superclass calling, your subclass can accept more or fewer arguments than its superclass, and can decide itself what to pass when calling the superclass.
这篇关于理解在子类中调用父 __init__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!