简单的Python继承

问题描述 投票:0回答:3
class Animal(object):
    def __init__(self, nlegs=4):
        self.nlegs = nlegs
    
class Cat(Animal):
    def __init__(self, talk='meow'):
        self.talk = talk

class Dog(Animal):
    def __init__(self, talk='woof'):
        self.talk = talk
  1. 为什么我的猫
    tom = Cat()
    没有
    nlegs
    属性?
  2. 我们应该从
    Animal.__init__()
    中显式调用
    Cat.__init__
    ,还是应该在这里使用
    super
  3. 如果我们想创建一只有 5 条腿的猫,我们是否应该向
    Cat.__init__
    接口添加额外的参数?
python inheritance constructor
3个回答
16
投票

要以其他人所说的为基础,是的,您需要调用父级的

__init__
方法。

一般最好使用super。但是,在某些情况下(特别是当您从多个类继承时),这可能是一个大问题。我将避免详细介绍,不乏讨论它的各种文章。 (此外,其他一些“特殊”功能也有一些奇怪之处。例如,您可以执行

super(SomeCls, self).__getitem__(5)
super(SomeCls, self)[5]
不起作用。)

作为一个简单的例子来说明为什么使用它是一个好主意,您可以使

Dog
Cat
继承自
Mammal
(继承自
Animal
),并且不必更改代码中的位置,除了类
Dog
Cat
继承自.

至于为什么你的

tom
实例没有
tom.nlegs
,那是因为你没有调用
Animal
__init__
方法。

还要记住,并非所有内容都需要在初始化时设置。对于这个例子,在

nlegs
方法中不要设置像
__init__
这样的东西更有意义。相反,直接在类中设置即可。例如

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

基本上,如果某些内容可能会在实例之间发生变化(例如猫的颜色)或需要在初始化时完成(例如打开文件),那么它可能应该在

__init__
中设置。

否则,如果我们希望类的任何实例都相同,那么直接在类定义中设置它会更干净。

此外,以这种方式设置的属性将可用于文档工具(例如内置的

help
函数),而在初始化时设置的属性则不可用。


4
投票

使用

super

class Cat(Animal):
    def __init__(self, talk='meow', num_legs=4):
        print 'Hay cat'
        self.talk = talk
        super(Cat, self).__init__(num_legs)


tom = Cat()  #tom is a normal cat
bob = Cat('Nyan', 3) #bob is a japanese defective cat

2
投票

您需要查看有关

super()
的 Python 文档。例如,您通常会通过调用
Cat.__init__()
来开始(或结束)您的
super(Cat, self).__init__(<any args>)
方法。

© www.soinside.com 2019 - 2024. All rights reserved.