super()的第二个参数? [英] Second parameter of super()?

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

问题描述

我的一位同事今天写了类似于以下内容的代码,要求我看看,花了我一段时间才发现错误:

A colleague of mine wrote code analogous to the following today, asked me to have a look, and it took me a while to spot the mistake:

class A():                                                                                         
    def __init__(self):                                                         
        print('A')                                                              

class B(A):                                                                     
    def __init__(self):                                                         
        super(B).__init__()                                               

b = B()

这里的问题是在 super()中没有 self 参数code> B 的构造函数。令我惊讶的是,在这种情况下绝对没有任何反应,即没有错误,没有任何反应。 super(B)创建的 super 对象包含什么?作为一个对象,它显然具有构造函数,因此就是所谓的构造函数,但是该对象与 B 有什么关系?特别是为什么这个有效的代码为什么没有在某处抛出异常? super(B)是具有一定实际用途的对象吗?

The problem here is that there's no self parameter to super() in B's constructor. What surprised me is that absolutely nothing happens in this case, i.e. no error, nothing. What does the super object created by super(B) contain? As an object, it clearly has a constructor, so that's what gets called, but how is that object related to B? In particular, why is this valid code and doesn't throw an exception somewhere? Is super(B) an object with some actual use and what would that be?

推荐答案

引起所有这些歧义的唯一原因是为什么 obj = super(B).__ init __()起作用?。这是因为 super(B).__ self_class __ 返回 None ,在这种情况下,您称为对象' __ init __ ,如下所示,返回无:

The only thing that causes all these ambiguities is that "why obj = super(B).__init__() works?". That's because super(B).__self_class__ returns None and in that case you're calling the None objects' __init__ like following which returns None:

In [40]: None.__init__()

关于其余案例,您只需在两种情况下调用 super 的基本属性即可检查差异:

Regarding the rest of the cases, you can simply check the difference by calling the super's essential attributes in both cases:

In [36]: class B(A):                                                                     
        def __init__(self):                                                         
                obj = super(B, self)
                print(obj.__class__)
                print(obj.__thisclass__)
                print(obj.__self_class__)
                print(obj.__self__)
   ....:         

In [37]: b = B()
<class 'super'>
<class '__main__.B'>
<class '__main__.B'>
<__main__.B object at 0x7f15a813f940>

In [38]: 

In [38]: class B(A):                                                                     
        def __init__(self):                                                         
                obj = super(B)
                print(obj.__class__)
                print(obj.__thisclass__)
                print(obj.__self_class__)
                print(obj.__self__)
   ....:         

In [39]: b = B()
<class 'super'>
<class '__main__.B'>
None
None

对于其他事情,我建议您阅读文档彻底。 https://docs.python.org/3/library/functions.html#超级,以及Raymond Hettinger的这篇文章 https:// rhettinger.wordpress.com/2011/05/26/super-considered-super/

For the rest of the things I recommend you to read the documentation thoroughly. https://docs.python.org/3/library/functions.html#super and this article by Raymond Hettinger https://rhettinger.wordpress.com/2011/05/26/super-considered-super/.

此外,如果您想知道为什么 super(B)在类之外不起作用,通常为什么在没有任何参数的情况下调用 super()可以在您的类中工作可以阅读Martijn https://stackoverflow.com/a/19609168/2867928 的综合答案。

Moreover, If you want to know why super(B) doesn't work outside of the class and generally why calling the super() without any argument works inside a class you can read This comprehensive answer by Martijn https://stackoverflow.com/a/19609168/2867928.

解决方案的简短描述:

如@NathanVērzemnieks的评论中所述,您需要调用一次初始化程序以获取 super()对象的工作。原因在于前面提到的链接中解释的新 super 对象的神奇之处。

As mentioned in the comments by @Nathan Vērzemnieks you need to call the initializer once to get the super() object work. The reason is laid behind the magic of new super object that is explained in aforementioned links.

In [1]: class A:
   ...:     def __init__(self):
   ...:         print("finally!")
   ...:

In [2]: class B(A):
   ...:     def __init__(self):
   ...:         sup = super(B)
   ...:         print("Before: {}".format(sup))
   ...:         sup.__init__()
   ...:         print("After: {}".format(sup))
   ...:         sup.__init__()
   ...:

In [3]: B()
Before: <super: <class 'B'>, NULL>
After: <super: <class 'B'>, <B object>>
finally!

这篇关于super()的第二个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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