将 super 与类方法一起使用
我正在尝试学习 Python 中的 super() 函数。
我以为我已经掌握了它,直到我看到这个例子(2.6)并发现自己陷入困境。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 9, in do_something
do_something = classmethod(do_something)
TypeError: unbound method do_something() must be called with B instance as first argument (got nothing instead)
>>>
当我在示例之前读到这一行时,这不是我所期望的:
如果我们'当使用类方法时,我们没有调用 super 的实例。对我们来说幸运的是,即使将类型作为第二个参数, super 也能工作。 --- 类型可以直接传递给 super ,如下所示。
Python 告诉我,应该用 B 的实例调用 do_something() ,这正是不可能的。
I'm trying to learn the super() function in Python.
I thought I had a grasp of it until I came over this example (2.6) and found myself stuck.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 9, in do_something
do_something = classmethod(do_something)
TypeError: unbound method do_something() must be called with B instance as first argument (got nothing instead)
>>>
It wasn't what I expected when I read this line right before the example:
If we're using a class method, we don't have an instance to call super with. Fortunately for us, super works even with a type as the second argument. --- The type can be passed directly to super as shown below.
Which is exactly what Python tells me is not possible by saying that do_something() should be called with an instance of B.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有时,必须更多地阅读文本才能了解想法的风格,而不是细节。这是其中之一。
在链接页面中,示例2.5、2.6和2.7都应该使用一种方法,
do_your_stuff
。 (也就是说,do_something
应更改为do_your_stuff
。)此外,如 Ned Deily 指出,
A.do_your_stuff
必须是一个类方法。super(B, cls).do_your_stuff
返回绑定方法(请参阅脚注 2 )。由于
cls
作为第二个参数传递给super()
,因此cls
绑定到返回的方法。换句话说,cls
作为第一个参数传递给类 A 的do_your_stuff()
方法。重申一下:
super(B, cls).do_your_stuff( )
导致A
的do_your_stuff
方法成为使用作为第一个参数传递的 cls 进行调用。为了使其发挥作用,
A
的do_your_stuff
必须是类方法。链接页面没有提到这一点,但事实确实如此。
附言。
do_something = classmethod(do_something)
是创建类方法的旧方法。新的方法是使用 @classmethod 装饰器。
请注意,
super(B, cls)
不能被super(cls, cls)
替换。这样做可能会导致无限循环。例如,将引发
RuntimeError:调用 Python 对象时超出最大递归深度
。如果
cls
是C
,则super(cls, cls)
会在C.mro()
中搜索以下类:位于C
之后。由于该类是
B
,当cls
是C
时,super(cls, cls).do_your_stuff()
<强>总是调用B.do_your_stuff
。由于super(cls, cls).do_your_stuff()
是在B.do_your_stuff
内部调用的,因此您最终会在无限循环中调用B.do_your_stuff
。在 Python3 中,0 参数形式的
super
是为了让super(B, cls)
可以被super()
替换,并且 Python3 会从上下文中找出super()
class B
定义中的 > 应该等同于super(B, cls)
。但在任何情况下
super(cls, cls)
(或出于类似原因,super(type(self), self)
)都是正确的。Sometimes texts have to be read more for the flavor of the idea rather than for the details. This is one of those cases.
In the linked page, Examples 2.5, 2.6 and 2.7 should all use one method,
do_your_stuff
. (That is,do_something
should be changed todo_your_stuff
.)In addition, as Ned Deily pointed out,
A.do_your_stuff
has to be a class method.super(B, cls).do_your_stuff
returns a bound method (see footnote 2). Since
cls
was passed as the second argument tosuper()
, it iscls
that gets bound to the returned method. In other words,cls
gets passed as the first argument to the methoddo_your_stuff()
of class A.To reiterate:
super(B, cls).do_your_stuff()
causesA
'sdo_your_stuff
method to becalled with
cls
passed as the first argument. In order for that to work,A
'sdo_your_stuff
has to be a class method. The linked page doesn't mention that,but that is definitively the case.
PS.
do_something = classmethod(do_something)
is the old way of making a classmethod.The new(er) way is to use the @classmethod decorator.
Note that
super(B, cls)
can not be replaced bysuper(cls, cls)
. Doing so could lead to infinite loops. For example,will raise
RuntimeError: maximum recursion depth exceeded while calling a Python object
.If
cls
isC
, thensuper(cls, cls)
searchesC.mro()
for the class that comes afterC
.Since that class is
B
, whencls
isC
,super(cls, cls).do_your_stuff()
always callsB.do_your_stuff
. Sincesuper(cls, cls).do_your_stuff()
is called insideB.do_your_stuff
, you end up callingB.do_your_stuff
in an infinite loop.In Python3, the 0-argument form of
super
was added sosuper(B, cls)
could be replaced bysuper()
, and Python3 will figure out from context thatsuper()
in the definition ofclass B
should be equivalent tosuper(B, cls)
.But in no circumstance is
super(cls, cls)
(or for similar reasons,super(type(self), self)
) ever correct.在 Python 3 中,您可以跳过为
super
指定参数,In Python 3, you can skip specifying arguments for
super
,我更新了这篇文章,使其更加清晰: Python属性和方法 # Super
上面使用 classmethod 的示例显示了类方法是什么 - 它传递类本身而不是实例作为第一个参数。但您甚至不需要实例来调用该方法,例如:
I've updated the article to make it a bit clearer: Python Attributes and Methods # Super
Your example using classmethod above shows what a class method is - it passes the class itself instead of the instance as the first parameter. But you don't even need an instance to call the method, for e.g.:
网页上的示例似乎可以按发布的方式工作。您是否也为超类创建了一个
do_something
方法,但没有将其放入类方法中?像这样的事情会给你这个错误:The example from the web page seems to work as published. Did you create a
do_something
method for the superclass as well but not make it into a classmethod? Something like this will give you that error:我想我现在已经明白了这一点,感谢这个美丽的网站和可爱的社区。
如果您不介意,如果我在类方法上错了(我现在正在尝试完全理解),请纠正我:
我希望这个插图显示..
I think I've understood the point now thanks to this beatiful site and lovely community.
If you don't mind please correct me if I'm wrong on classmethods (which I am now trying to understand fully):
I hope this illustration shows ..