使用super和类方法 [英] Using super with a class method

查看:124
本文介绍了使用super和类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习Python中的super()函数。

虽然我掌握了它, (2.6),发现自己陷入了困境。

I though I got a grasp of it untill I came over this example (2.6) and found myself stuck.

http://www.cafepy.com/article/python_attributes_and_methods/python_attributes_and_methods.html#super-with-classmethod-example

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:

如果我们使用类方法,我们没有一个实例来调用super。对我们来说幸运的是,super甚至用类型作为第二个参数。 ---类型可以直接传递给super,如下所示。
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.

这正是Python告诉我的是不可能的,do_something()应该用B的实例调用。

Which is exactly what Python tells me is not possible by saying that do_something() should be called with an instance of B.

提前感谢

推荐答案

有时候,的想法,而不是细节。这是其中一种情况。

Sometimes texts have to be read more for the flavor of the idea rather than for the details. This is one of those cases.

linked page ,示例2.5,2.6和2.7都应使用一个方法 do_your_stuff 。 (也就是说, do_something 应该更改为 do_your_stuff 。)

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 to do_your_stuff.)

另外,由于 Ned Deily指出 A.do_your_stuff 必须是类方法。

In addition, as Ned Deily pointed out, A.do_your_stuff has to be a class method.

class A(object):
    @classmethod
    def do_your_stuff(cls):
        print 'This is A'

class B(A):
    @classmethod
    def do_your_stuff(cls):
        super(B, cls).do_your_stuff()

B.do_your_stuff()

super(B,cls).do_your_stuff
返回一个绑定方法(参见 footnote 2 )。由于 cls 作为第二个参数传递给 super(),因此 cls 绑定到返回的方法。换句话说, cls 作为方法的第一个参数传递 do_you_stuff()

super(B, cls).do_your_stuff returns a bound method (see footnote 2). Since cls was passed as the second argument to super(), it is cls that gets bound to the returned method. In other words, cls gets passed as the first argument to the method do_you_stuff().

因此 super(B,cls).do_your_stuff()导致 A do_your_stuff 方法调用
,并将 cls 作为第一个参数传递。为了使它工作, A
do_your_stuff 必须是类方法。链接的页面没有提到,
,但它是真的。

So super(B, cls).do_your_stuff() causes A's do_your_stuff method to be called with cls passed as the first argument. In order for that to work, A's do_your_stuff has to be a class method. The linked page doesn't mention that, but it's true.

PS。 do_something = classmethod(do_something)是制作类方法的旧方法。
新的(er)方法是使用@classmethod装饰器。

PS. do_something = classmethod(do_something) is the old way of making a classmethod. The new(er) way is to use the @classmethod decorator.

这篇关于使用super和类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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