super()是不是要用于staticmethods?
当我尝试类似的东西
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
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
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'
Run Code Online (Sandbox Code Playgroud)
如果我将staticmethods更改为classmethods并将类实例传递给super(),那么一切正常.我在这里不正确地调用超级(类型)还是有些东西我不见了?
嗨我得到这个错误
TypeError: attack() missing 1 required positional argument: 'self'
Run Code Online (Sandbox Code Playgroud)
这是我的代码
class Enemmy :
life = 3
self = ""
def attack(self):
print ("ouch!!!!")
self.life -= 1
def checkLife(self):
if self.life <= 0 :
print ("dead")
else:
print (self.life)
enemy=Enemmy
enemy.attack()
Run Code Online (Sandbox Code Playgroud)
我检查并查看大多数地方说我忘了自我在def攻击或我需要做一个obj把我的类使用python 3.4与py魅力我实际上从教程得到这个代码,我不知道我的是什么错误