Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

python - super() and @staticmethod interaction

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...