Python继承:何时和为什么__init__ [英] Python inheritance: when and why __init__

查看:84
本文介绍了Python继承:何时和为什么__init__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,试图了解继承方法的原理/逻辑.问题最终涉及为什么以及何时必须在子类中使用__init__方法.示例:

I'm a Python newbie, trying to understand the philosophy/logic behind the inheritance methods. Questions ultimately regards why and when one has to use the __init__ method in a subclass. Example:

似乎从超类继承的子类不必具有自己的构造函数(c1>)方法.在下面,狗继承了哺乳动物的属性(名称,年龄)和方法(makenoise).您甚至可以添加一个方法(do_a_trick),看来一切都应按应该"的方式进行.

It seems a subclass inheriting from a superclass need not have its own constructor (__init__) method. Below, a dog inherits the attributes (name, age) and methods (makenoise) of a mammal. You can even add a method (do_a_trick) Everything works as it ``should", it seems.

但是,如果我想在Cats类中尝试在子类中添加新属性,则会收到一条错误消息,提示未定义自我".但是我在狗类的定义中使用了自我".差异的本质是什么? 好像我希望使用__init__(self,name)super()__init__(name)来定义Cats.为什么会有所不同?

However, if I wanted to add a new attribute in the subclass as I attempt to do in the Cats class, I get an error saying "self" is not defined. Yet I used "self" in the definition of the dog class. What's the nature of the difference? It seems to define Cats as I wish I need to use __init__(self,name) and super()__init__(name). Why the difference?

class Mammals(object):

  def __init__(self,name):
    self.name = name
    print("I am a new-born "+ self.name)  
    self.age = 0

  def makenoise(self):
    print(self.name + " says Hello")

class Dogs(Mammals):

  def do_a_trick(self):
    print(self.name + " can roll over")

class Cats(Mammals):

 self.furry = "True"  #results in error `self' is not defined


mymammal = Mammals("zebra") #output "I am a new-born zebra"
mymammal.makenoise()  #output "zebra says hello"
print(mymmmal.age)    #output 0

mydog = Dogs("family pet") #output "I am a new-born family pet"
mydog.makenoise()  #output "family pet says hello"
print(mydog.age)  # output 0
mydog.do_a_trick() #output "family pet can roll over"

推荐答案

如果我想在子类中添加新属性,请尝试 在Cats类中,我收到一条错误消息,提示未定义自我".可是我 在狗类的定义中使用了自我".

if I wanted to add a new attribute in the subclass as I attempt to do in the Cats class, I get an error saying "self" is not defined. Yet I used "self" in the definition of the dog class.

在您的超类Mammal中,您有一个__init__函数,该函数采用您选择的参数*来调用self.当您位于__init__函数的正文中时,此参数在范围内-它是一个局部变量,与任何局部变量一样,并且在其包含函数终止后无法引用它. 在Dog类do_a_trick上定义的函数也采用称为self的参数,并且对于该函数也是局部的. 使这些变量与众不同的不是它们的名称(您可以将其命名为所需的任何名称),而是这样的事实,即作为python中实例方法的第一个参数,它们获得了对被称为其值的对象的引用. (再读一遍最后一句话,这是理解这一点的关键,您可能第一次不会理解它.)

In your superclass, Mammal, you have an __init__ function, which takes an argument that you've chosen* to call self. This argument is in scope when you're in the body of the __init__ function - it's a local variable like any local variable, and it's not possible to refer to it after its containing function terminates. The function defined on the Dog class, do_a_trick, also takes an argument called self, and it is also local to that function. What makes these variables special is not their name (you could call them anything you wanted) but the fact that, as the first arguments to instance methods in python, they get a reference to the object on which they're called as their value. (Read that last sentence again a few times, it's the key to understanding this, and you probably won't get it the first time.)

现在,在Cat中,您有一行代码根本不在函数中.目前没有任何内容,包括self,这就是失败的原因.如果要在Cat中定义一个接受名为self的参数的函数,则可以引用该参数.如果该参数碰巧是Cat上实例方法的第一个参数,则它将具有在其上被调用的Cat实例的值.否则,它将具有传递给它的任何东西.

Now, in Cat, you have a line of code which is not in a function at all. Nothing is in scope at this point, including self, which is why this fails. If you were to define a function in Cat that took an argument called self, it would be possible to refer to that argument. If that argument happened to be the first argument to an instance method on Cat, then it would have the value of the instance of Cat on which it had been called. Otherwise, it would have whatever got passed to it.

*您的选择明智!

这篇关于Python继承:何时和为什么__init__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆