在python中调用超类的__init__时显式传递Self [英] Explicit passing of Self when calling super class's __init__ in python

查看:48
本文介绍了在python中调用超类的__init__时显式传递Self的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题与 超级"在 Python 中的作用有关? , 我如何初始化基础 (super) 类?Python:如何从超类创建子类? 描述了从 SubClass 中将 SuperClass 初始化为

This question is in relation to posts at What does 'super' do in Python? , How do I initialize the base (super) class? , and Python: How do I make a subclass from a superclass? which describes two ways to initialize a SuperClass from within a SubClass as

class SuperClass:
    def __init__(self):
        return
    def superMethod(self):
        return


## One version of Initiation
class SubClass(SuperClass):
    def __init__(self):
        SuperClass.__init__(self)
    def subMethod(self):
        return

class SuperClass:
    def __init__(self):
        return
    def superMethod(self):
        return

## Another version of Initiation
class SubClass(SuperClass):
    def __init__(self):
        super(SubClass, self).__init__()
    def subMethod(self):
        return

所以我对需要显式传递self作为参数有点困惑SuperClass.__init__(self)super(SubClass, self).__init__().(事实上​​,如果我调用 SuperClass.__init__() 我得到错误

So I'm a little confused about needing to explicitly pass self as a parameter in SuperClass.__init__(self) and super(SubClass, self).__init__(). (In fact if I call SuperClass.__init__() I get the error

TypeError: __init__() missing 1 required positional argument: 'self'

).但是当调用构造函数或任何其他类方法时(即:

). But when calling constructors or any other class method (ie :

## Calling class constructor / initiation
c = SuperClass()
k = SubClass()

## Calling class methods
c.superMethod()
k.superMethod()
k.subMethod()

),self 参数被隐式传递.

我对 self 关键字的理解是它与 C++ 中的 this 指针没有什么不同,但它提供了对类实例的引用.这是正确的吗?

My understanding of the self keyword is it is not unlike the this pointer in C++, whereas it provides a reference to the class instance. Is this correct?

如果总是有一个当前实例(在本例中为 SubClass),那么为什么 self 需要明确地包含在对 SuperClass 的调用中.__init__(self)?

If there would always be a current instance (in this case SubClass), then why does self need to be explicitly included in the call to SuperClass.__init__(self)?

谢谢

推荐答案

这只是方法绑定,与 super 关系不大.当您可以 x.method(*args) 时,Python 会检查 x 的类型以查找名为 method 的方法.如果找到,它会将函数绑定"到 x,这样当你调用它时,x 将作为第一个参数传递,在其余参数之前.

This is simply method binding, and has very little to do with super. When you can x.method(*args), Python checks the type of x for a method named method. If it finds one, it "binds" the function to x, so that when you call it, x will be passed as the first parameter, before the rest of the arguments.

当您通过其类调用(普通)方法时,不会发生此类绑定.如果该方法期望它的第一个参数是一个实例(例如 self),则您需要自己传入它.

When you call a (normal) method via its class, no such binding occurs. If the method expects its first argument to be an instance (e.g. self), you need to pass it in yourself.

这种绑定行为的实际实现非常简洁.如果 Python 对象具有 __get__ 方法(和/或 __set____delete__ 方法,但这些方法对方法无关紧要),则它们是描述符").当您查找像 a.b 这样的属性时,Python 会检查 a 的类以查看它是否具有作为描述符的属性 b.如果是,它会将 a.b 翻译成 type(a).b.__get__(a, type(a)).如果 b 是一个函数,它将有一个 __get__ 方法来实现我上面描述的绑定行为.其他类型的描述符可以有不同的行为.例如,classmethod 装饰器用一个特殊的描述符替换方法,该描述符将函数绑定到类,而不是实例.

The actual implementation of this binding behavior is pretty neat. Python objects are "descriptors" if they have a __get__ method (and/or __set__ or __delete__ methods, but those don't matter for methods). When you look up an attribute like a.b, Python checks the class of a to see if it has a attribute b that is a descriptor. If it does, it translates a.b into type(a).b.__get__(a, type(a)). If b is a function, it will have a __get__ method that implements the binding behavior I described above. Other kinds of descriptors can have different behaviors. For instance, the classmethod decorator replaces a method with a special descriptor that binds the function the class, rather than the instance.

Python 的 super 创建特殊对象,其处理属性查找的方式与普通对象不同,但细节对于这个问题并不重要.通过super调用的方法的绑定行为就像我在第一段中描述的那样,所以self在调用时会自动传递给绑定的方法.super 的唯一特别之处在于它可能绑定一个不同的函数,而不是在 self 上查找相同的方法名称(这就是使用它的全部意义所在).

Python's super creates special objects that handle attribute lookups differently than normal objects, but the details don't matter too much for this issue. The binding behavior of methods called through super is just like what I described in the first paragraph, so self gets passed automatically to the bound method when it is called. The only thing special about super is that it may bind a different function than you'd get lookup up the same method name on self (that's the whole point of using it).

这篇关于在python中调用超类的__init__时显式传递Self的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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