super() 和@staticmethod 交互 [英] super() and @staticmethod interaction

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

问题描述

super() 不打算与静态方法一起使用吗?

当我尝试类似的事情时

class First(object):@静态方法定义获取列表():返回 ['第一个']第二课(第一课):@静态方法定义获取列表():l = super(Second).getlist()l.append('第二个')返回 la = Second.getlist()打印一个

我收到以下错误

回溯(最近一次调用最后一次): 中的文件asdf.py",第 13 行a = Second.getlist()文件asdf.py",第 9 行,在 getlist 中l = super(Second).getlist()AttributeError: 'super' 对象没有属性 'getlist'

如果我将 staticmethods 更改为 classmethods 并将类实例传递给 super(),则一切正常.我在这里错误地调用了 super(type) 还是我遗漏了什么?

解决方案

简短回答

<块引用>我在这里错误地调用了 super(type) 还是我遗漏了什么?

是:是的,您的称呼有误……而且(确实,因为)您缺少某些东西.

但是不要难过;这是一个极其困难的主题.

文档指出

<块引用>如果省略第二个参数,则返回的超级对象未绑定.

未绑定 super 对象的用例极其狭窄和罕见.有关 super() 的讨论,请参阅 Michele Simionato 的这些文章:

此外,他强烈主张从 Python 3 这里.

我说过您错误地"称其为(尽管没有上下文,正确性在很大程度上毫无意义,而玩具示例并没有提供太多上下文).因为未绑定的 super 非常少见,而且可能完全不合理,正如 Simionato 所说,使用 super() 的正确"方法是提供第二个参数.

就您而言,使您的示例工作的最简单方法是

class First(object):@静态方法定义获取列表():返回 ['第一个']第二课(第一课):@静态方法定义获取列表():l = super(Second, Second).getlist() # 注意第二个参数l.append('第二个')返回 la = Second.getlist()打印一个

如果你认为这样看起来很有趣,那你就没有错.但我认为大多数人在看到 super(X) 时所期待的(或者希望他们在自己的代码中尝试时)正是 Python 给你的,如果你执行 super(X,X).

Is super() not meant to be used with staticmethods?

When I try something like

class First(object):
  @staticmethod
  def getlist():
    return ['first']

class Second(First):
  @staticmethod
  def getlist():
    l = super(Second).getlist()
    l.append('second')
    return l

a = Second.getlist()
print a

I get the following error

Traceback (most recent call last):
  File "asdf.py", line 13, in <module>
    a = Second.getlist()
  File "asdf.py", line 9, in getlist
    l = super(Second).getlist()
AttributeError: 'super' object has no attribute 'getlist'

If I change the staticmethods to classmethods and pass the class instance to super(), things work fine. Am I calling super(type) incorrectly here or is there something I'm missing?

解决方案

The short answer to

Am I calling super(type) incorrectly here or is there something I'm missing?

is: yes, you're calling it incorrectly... AND (indeed, because) there is something you're missing.

But don't feel bad; this is an extremely difficult subject.

The documentation notes that

If the second argument is omitted, the super object returned is unbound.

The use case for unbound super objects is extremely narrow and rare. See these articles by Michele Simionato for his discussion on super():

Also, he argues strongly for removing unbound super from Python 3 here.

I said you were calling it "incorrectly" (though correctness is largely meaningless without context, and a toy example doesn't give much context). Because unbound super is so rare, and possibly just flat-out unjustified, as argued by Simionato, the "correct" way to use super() is to provide the second argument.

In your case, the simplest way to make your example work is

class First(object):
  @staticmethod
  def getlist():
    return ['first']

class Second(First):
  @staticmethod
  def getlist():
    l = super(Second, Second).getlist()  # note the 2nd argument
    l.append('second')
    return l

a = Second.getlist()
print a

If you think it looks funny that way, you're not wrong. But I think what most people are expecting when they see super(X) (or hoping for when they try it in their own code) is what Python gives you if you do super(X, X).

这篇关于super() 和@staticmethod 交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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