__init__是一个类方法吗? [英] Is __init__ a class method?

查看:148
本文介绍了__init__是一个类方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Python的超级方法和多重继承.我读到类似的内容,例如当我们使用super调用在所有基类中都有实现的基方法时,即使具有各种参数,也只会调用一个类的方法.例如,

I was looking into Python's super method and multiple inheritance. I read along something like when we use super to call a base method which has implementation in all base classes, only one class' method will be called even with variety of arguments. For example,

class Base1(object):
    def __init__(self, a):
        print "In Base 1"

class Base2(object):
    def __init__(self):
        print "In Base 2"

class Child(Base1, Base2):
    def __init__(self):
        super(Child, self).__init__('Intended for base 1')
        super(Child, self).__init__()# Intended for base 2

这将为第一个super方法生成TyepError. super会调用它首先识别并给出TypeError的任何方法实现,而不是检查后续的其他类.但是,当我们执行以下操作时,这将更加清楚并且可以正常工作:

This produces TyepError for the first super method. super would call whichever method implementation it first recognizes and gives TypeError instead of checking for other classes down the road. However, this will be much more clear and work fine when we do the following:

class Child(Base1, Base2):
    def __init__(self):
        Base1.__init__(self, 'Intended for base 1')
        Base2.__init__(self) # Intended for base 2

这导致两个问题:

  1. __init__方法是静态方法还是类方法?
  2. 为什么要使用super,它会自己隐式选择方法,而不是像后面的示例那样显式调用方法?对我来说,它看起来比使用super更干净.那么在第二种方法中使用super有什么好处(除了通过方法调用编写基类名称之外)
  1. Is __init__ method a static method or a class method?
  2. Why use super, which implicitly choose the method on it's own rather than explicit call to the method like the latter example? It looks lot more cleaner than using super to me. So what is the advantage of using super over the second way(other than writing the base class name with the method call)

推荐答案

super()面对多重继承,尤其是在object

super() in the face of multiple inheritance, especially on methods that are present on object can get a bit tricky. The general rule is that if you use super, then every class in the hierarchy should use super. A good way to handle this for __init__ is to make every method take **kwargs, and always use keyword arguments everywhere. By the time the call to object.__init__ occurs, all arguments should have been popped out!

class Base1(object):
    def __init__(self, a, **kwargs):
        print "In Base 1", a
        super(Base1, self).__init__()

class Base2(object):
    def __init__(self, **kwargs):
        print "In Base 2"
        super(Base2, self).__init__()

class Child(Base1, Base2):
    def __init__(self, **kwargs):
        super(Child, self).__init__(a="Something for Base1")

有关更多信息,请参见链接的文章,以及如何使其更适合您!

See the linked article for way more explanation of how this works and how to make it work for you!

冒着回答两个问题的风险:为什么要完全使用超级?"

At the risk of answering two questions, "Why use super at all?"

由于具有类和继承的许多相同原因,我们拥有super()作为用于模块化和抽象化代码的工具.在类的实例上进行操作时,您不需要了解该类的实现方式的所有详细信息,只需要了解其方法和属性以及使用该公共接口的方式即可.上课.特别是,您可以确信,对类的实现进行的更改不会以实例用户的身份给您带来麻烦.

We have super() for many of the same reasons we have classes and inheritance, as a tool for modularizing and abstracting our code. When operating on an instance of a class, you don't need to know all of the gritty details of how that class was implemented, you only need to know about its methods and attributes, and how you're meant to use that public interface for the class. In particular, you can be confident that changes in the implementation of a class can't cause you problems as a user of its instances.

从基类派生新类型时,使用相同的参数.您不需要或不必担心这些基类是如何实现的.这是不使用super可能会出错的一个具体示例.假设您有:

The same argument holds when deriving new types from base classes. You don't want or need to worry about how those base classes were implemented. Here's a concrete example of how not using super might go wrong. suppose you've got:

class Foo(object):
     def frob(self):
         print "frobbign as a foo"
class Bar(object):
     def frob(self):
         print "frobbign as a bar"

然后创建一个子类:

class FooBar(Foo, Bar):
    def frob(self):
        Foo.frob(self)
        Bar.frob(self)

一切都很好,但是随后您意识到当您沉迷于此时, Foo确实是Bar的一种,所以您可以对其进行更改

Everything's fine, but then you realize that when you get down to it, Foo really is a kind of Bar, so you change it

 class Foo(Bar):
     def frob(self):
         print "frobbign as a foo"
         Bar.frob(self)

一切都很好,除了在您的派生类中,FooBar.frob()调用Bar.frob() 两次.

Which is all fine, except that in your derived class, FooBar.frob() calls Bar.frob() twice.

这是super()解决的确切问题,它可以防止您多次调用超类实现(按指示使用时...)

This is the exact problem super() solves, it protects you from calling superclass implementations more than once (when used as directed...)

这篇关于__init__是一个类方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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