super()究竟在Python 3中返回什么? [英] What exactly does super() return in Python 3?

查看:150
本文介绍了super()究竟在Python 3中返回什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Python3的文档 super()返回一个代理对象,该方法将方法调用委托给父类或兄弟类的类型。这是什么意思?

From Python3's documentation super() "returns a proxy object that delegates method calls to a parent or sibling class of type." What does that mean?

假设我有以下代码:

class SuperClass():
    def __init__(self):
        print("__init__ from SuperClass.")
        print("self object id from SuperClass: " + str(id(self)))

class SubClass(SuperClass):
    def __init__(self):
        print("__init__ from SubClass.")
        print("self object id from SubClass: " + str(id(self)))
        super().__init__()


sc = SubClass()

我得到的输出是:


__init__ from SubClass.
self object id from SubClass: 140690611849200
__init__ from SuperClass.
self object id from SuperClass: 140690611849200

这意味着行 super().__ init__ () super()返回当前对象,然后将其隐式传递给超类' __ init __()方法。这是准确的还是我在这里遗漏了什么?

This means that in the line super().__init__(), super() is returning the current object which is then implicitly passed to the superclass' __init__() method. Is this accurate or am I missing something here?

简单来说,我想了解以下内容:

To put it simply, I want to understand the following:

super().__ init __()运行时,


  1. 到底是什么正被传递给 __ init __()以及如何?我们在 super()上调用它,所以无论返回什么,都应该传递给 __ init __()方法到目前为止我对Python的理解。

  2. 为什么我们不必将 self 传递给 super ().__ init __()

  1. What exactly is being passed to __init__() and how? We are calling it on super() so whatever this is returning should be getting passed to the __init__() method from what I understand about Python so far.
  2. Why don't we have to pass in self to super().__init__()?


推荐答案


返回一个代理对象,该方法将方法调用委托给父类或
兄弟类的类型。

returns a proxy object that delegates method calls to a parent or sibling class of type.

proxy 是一个对象,它充当父类的方法调用部分。这不是班级本身;相反,它只是足够的信息,以便您可以使用它来调用父类方法。

This proxy is an object that acts as the method-calling portion of the parent class. It is not the class itself; rather, it's just enough information so that you can use it to call the parent class methods.

如果你调用 __ init __(),你得到你自己的,本地的,子类 __ init __ 函数。当您调用 super()时,您将获得该代理对象,该对象将重定向到父类方法。因此,当您调用 super().__ init __()时,该代理会将调用重定向到父类 __ init __ 方法。

If you call __init__(), you get your own, local, sub-class __init__ function. When you call super(), you get that proxy object, which will redirect you to the parent-class methods. Thus, when you call super().__init__(), that proxy redirects the call to the parent-class __init__ method.

同样,如果你打电话给 super()。foo ,你会得到<$ c来自父类的$ c> foo 方法 - 再次由该代理重新路由。

Similarly, if you were to call super().foo, you would get the foo method from the parent class -- again, re-routed by that proxy.

这对你来说是否清楚?

对OP评论的回应


但这必须意味着当运行super()时,此代理对象被传递给
init ()。 init ()right ?

But that must mean that this proxy object is being passed to init() when running super().init() right?

错误。代理对象就像一个包名,比如调用math.sqrt()。你没有将数学传递给sqrt,你用它来表示你正在使用哪个sqrt。如果您想将代理传递给 init ,则调用将是 init (super())。当然,那个调用在语义上是荒谬的。

Wrong. The proxy object is like a package name, such as calling math.sqrt(). You're not passing math to sqrt, you're using it to denote which sqrt you're using. If you wanted to pass the proxy to init, the call would be init(super()). That call would be semantically ridiculous, of course.


当我们必须实际传入self时,我的例子中是sc对象。

When we have to actually pass in self which is the sc object in my example.

不,你传入 sc ;这是对象创建调用的结果(内部方法 __ new __ ),其中包括 init 。对于 __ init __ self 对象是Python运行时系统为您创建的新项目。对于大多数类方法,第一个参数(称为 self 超出约定,在其他语言中)是对象调用了这个方法。

No, you are not passing in sc; that is the result of the object creation call (internal method __new__), which includes an invocation of init. For __init__, the self object is a new item created for you by the Python run-time system. For most class methods, that first argument (called self out of convention, this in other languages) is the object that invoked the method.

这篇关于super()究竟在Python 3中返回什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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