不要从Python的超级中获得预期的结果

jdb*_*org -2 python

根据我的理解,该函数super应该允许嵌套在另一个中的Class访问其父级的'self'.我可能错了,但这里有一个我想要实现的简单例子:

class Test:
     def __init__(self):
         self.message = "Hello World"
     class Print:
         def __init__(self):
             print super(Test, self).message


this = Test()
this.Print()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/home1/users/joe.borg/<ipython-input-3-3eb5db70be43> in <module>()
----> 1 this.Print()

/home1/users/joe.borg/<ipython-input-1-cee67a2914c3> in __init__(self)
      4     class Print:
      5         def __init__(self):
----> 6             print super(Test, self).message
      7 

TypeError: must be type, not classobj
Run Code Online (Sandbox Code Playgroud)

jco*_*ado 5

super应该用于调用给定类的mro(方法解析顺序)中的下一个方法(通常是父类的方法).这与你想要做的完全不同.

除此之外,您的类是旧式类(它们不是子类对象),所以super抱怨因为它只适用于新式类.