简单的python继承 [英] Simple python inheritance
问题描述
class Animal(object):
def __init__(self, nlegs=4):
print '__init__ Animal'
self.nlegs = nlegs
class Cat(Animal):
def __init__(self, talk='meow'):
print '__init__ Cat'
self.talk = talk
class Dog(Animal):
def __init__(self, talk='woof'):
print '__init__ Dog'
self.talk = talk
- 为什么我的猫
tom = Cat()
没有nlegs属性? - 我们是否应该从
Cat .__ init __
Animal .__ init __() >,或者我们应该用super
做一些更有趣的事情? - 如果我想创建一条有5条腿的猫,我需要在
Cat .__ init __
界面中添加其他参数吗? ?
- Why does my cat
tom = Cat()
not have an nlegs attribute? - Should we explicitly call
Animal.__init__()
from withinCat.__init__
, or should we be doing something more fancy, say, withsuper
? - What about if I want to create a cat with 5 legs, do I need to add additional arguments to the
Cat.__init__
interface?
推荐答案
建立在其他人所说的基础之上,是的,你需要调用父的 __ init __
方法。
To build on what everyone else has said, yes, you'll need to call the parent's __init__
method.
通常最好使用super。但是,在某些情况下(特别是当你从多个类继承时)它可能是一个很大的问题。我将避免详细介绍,不缺少讨论它的各种文章。 (另外,其他一些特殊功能也有些奇怪。例如,你可以做 super(SomeCls,self).__ getitem __(5)
但是 super(SomeCls,self)[5]
将不起作用。)
It's generally best to use super. However, in certain cases (particularly when you're inheriting from multiple classes) it can be a big gotcha. I'll avoid going into detail, there are no shortage of various articles which discuss it. (Also, there are some oddities with some of the other "special" functions. For example, you can do super(SomeCls, self).__getitem__(5)
but super(SomeCls, self)[5]
won't work.)
作为一个简单的例子说明为什么它是好的想要使用它,你可以让狗
和猫
继承自哺乳动物
(继承自 Animal
)并且不必更改代码中除 Cat
继承自。
As a simplistic example of why it's a good idea to use it, you could make Dog
and Cat
inherit from Mammal
(which inherits from Animal
) and not have to change places in your code other than which class Dog
and Cat
inherit from.
至于为什么你的 tom
实例没有 tom.nlegs
,这是因为你没有调用 Animal
的 __ init __
方法。
As for why your tom
instance doesn't have tom.nlegs
, it's because you haven't called Animal
's __init__
method.
还要记住,并非所有内容都需要在初始化时设置。对于此示例,更有意义的是不在 __ init __
方法中设置 nlegs
之类的内容。相反,只需在类中直接设置它。例如
Also remember that not everything needs to be set at initialization time. For this example, it makes more sense not to set things like nlegs
in the __init__
method. Instead, just set it directly in the class. E.g.
class Mammal(object):
nlimbs = 4
def __init__(self):
print "I'm a mammal!"
class Cat(Mammal):
def __init__(self, color="calico"):
self.color = color
super(Cat, self).__init__()
print "I have {0} legs I am {1}".format(self.nlimbs, self.color)
class FiveLeggedCat(Cat):
nlimbs = 5
基本上,如果某些事情可能会在不同实例之间发生变化(例如cat)或者需要在初始化时完成(例如打开文件),然后它可能应该在 __ init __
中设置。
Basically, if something is likely to change from instance to instance (e.g. the color of the cat) or needs to be done at initialization (e.g. opening a file), then it probably should be set in __init__
.
否则,如果我们想要对类的任何实例都相同,那么直接在类定义中设置它可能更清晰。
Otherwise, if it's something we want to be the same for any instance of the class, it can be cleaner to set it directly in the class definition.
此外,以这种方式设置的属性将可用于文档工具(例如内置的 help
函数),而在初始化时设置的属性不会。
Also, attributes set this way will be available to documentation tools (e.g. the built-in help
function), whereas attributes set at initialization won't be.
这篇关于简单的python继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!