TypeError: Super does not take Key word arguments?
TypeError: Super does not take Key word arguments?
首先,这是我的代码:
class Enemy():
def __init__(self, name, hp, damage):
self.name = name
self.hp = hp
self.damage = damage
def is_alive(self):
"""Checks if alive"""
return self.hp > 0
class WildBoar(Enemy):
def __init__(self):
super(WildBoar, name="Wild Boar", hp=10, damage=2).__init__()
class Marauder(Enemy):
def __init__(self):
super(Marauder, name="Marauder", hp=20, damage=5).__init__()
class Kidnappers(Enemy):
def __init__(self):
super(Kidnappers, name="The Kidnappers", hp=30, damage=7).__init__()
当我编译这个时,我得到这个错误:
super(WildBoar, name="Wild Boar", hp=10, damage=2).__init__()
TypeError: super does not take keyword arguments
我尝试四处寻找任何帮助,但找不到任何帮助。我在其他一些 class 的超级中也有一些 Kwargs,但这些都是提出任何问题的(截至目前)。那么是什么原因造成的呢?我还看到有人说在基础 class 中放置一个 super
会修复它,但它没有用(我传递了与基础 class 中相同的参数的 __init__
).
父类 __init__
方法的参数应传递给 __init__
方法:
super(Kidnappers, self).__init__(name="The Kidnappers", hp=30, damage=7)
# or
super(Kidnappers, self).__init__("The Kidnappers", 30, 7)
您传递给 super()
的所有内容都是子 class(在本例中为 Kidnappers
)和对当前实例的引用 (self
)。
但是请注意,如果您使用 Python 3.x,您需要做的就是:
super().__init__("The Kidnappers", 30, 7)
和Python会解决剩下的问题。
这里有一些指向文档中对此进行解释的链接:
选项#1:Python 2.7x
在这里您可以将 self
keywork 传递给 super() ,它本身引用实例属性。
super(self, name="Wild Boar", hp=10, damage=2).__init__()
选项 # 2 : Python 3x
super()
不再需要任何参数,你可以简单地写
super().__init__("The Kidnappers", 30, 7)
首先,这是我的代码:
class Enemy():
def __init__(self, name, hp, damage):
self.name = name
self.hp = hp
self.damage = damage
def is_alive(self):
"""Checks if alive"""
return self.hp > 0
class WildBoar(Enemy):
def __init__(self):
super(WildBoar, name="Wild Boar", hp=10, damage=2).__init__()
class Marauder(Enemy):
def __init__(self):
super(Marauder, name="Marauder", hp=20, damage=5).__init__()
class Kidnappers(Enemy):
def __init__(self):
super(Kidnappers, name="The Kidnappers", hp=30, damage=7).__init__()
当我编译这个时,我得到这个错误:
super(WildBoar, name="Wild Boar", hp=10, damage=2).__init__()
TypeError: super does not take keyword arguments
我尝试四处寻找任何帮助,但找不到任何帮助。我在其他一些 class 的超级中也有一些 Kwargs,但这些都是提出任何问题的(截至目前)。那么是什么原因造成的呢?我还看到有人说在基础 class 中放置一个 super
会修复它,但它没有用(我传递了与基础 class 中相同的参数的 __init__
).
父类 __init__
方法的参数应传递给 __init__
方法:
super(Kidnappers, self).__init__(name="The Kidnappers", hp=30, damage=7)
# or
super(Kidnappers, self).__init__("The Kidnappers", 30, 7)
您传递给 super()
的所有内容都是子 class(在本例中为 Kidnappers
)和对当前实例的引用 (self
)。
但是请注意,如果您使用 Python 3.x,您需要做的就是:
super().__init__("The Kidnappers", 30, 7)
和Python会解决剩下的问题。
这里有一些指向文档中对此进行解释的链接:
选项#1:Python 2.7x
在这里您可以将 self
keywork 传递给 super() ,它本身引用实例属性。
super(self, name="Wild Boar", hp=10, damage=2).__init__()
选项 # 2 : Python 3x
super()
不再需要任何参数,你可以简单地写
super().__init__("The Kidnappers", 30, 7)