__init__() 得到了意外的关键字参数 'y'
__init__() got unexpected keyword argument 'y'
我正在学习这本书,'Python Programming For The Absolute Beginner' 并决定通过制作自己的游戏来测试我的一些技能。游戏基本上是 "don't get hit by the flying spikes",我遇到了一个问题。当 运行 它与此代码:
class Player(games.Sprite):
"""The player that must dodge the spikes."""
def update(self):
"""Move to the mouse."""
self.x = games.mouse.x
self.y = games.mouse.y
self.check_collide()
def check_collide(self):
"""Check for a collision with the spikes."""
for spike in self.overlapping_sprites:
spike.handle_collide()
def main():
pig_image = games.load_image("Mr_Pig.png")
the_pig = Player(image = pig_image,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(the_pig)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main()
我没问题。但是当我想使用'init'时,就像在这段代码中:
class Player(games.Sprite):
"""The player that must dodge the spikes."""
def update(self):
"""Move to the mouse."""
self.x = games.mouse.x
self.y = games.mouse.y
self.check_collide()
def check_collide(self):
"""Check for a collision with the spikes."""
for spike in self.overlapping_sprites:
spike.handle_collide()
def __init__(self):
"""A test!"""
print("Test.")
def main():
pig_image = games.load_image("Mr_Pig.png")
the_pig = Player(image = pig_image,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(the_pig)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main()
我在 运行 游戏时遇到此错误:
File "run.py", line 85, in main
y = games.mouse.y)
TypeError: __init__() got an unexpected keyword argument 'y'.
这一行:
the_pig = Player(image = pig_image,
x = games.mouse.x,
y = games.mouse.y)
(有点)等同于:
the_pig = Player.__init__(image = pig_image,
x = games.mouse.x,
y = games.mouse.y)
这意味着您的 __init__
应该接受参数 image
、x
和 y
,但您将其定义为:
def __init__(self):
"""A test!"""
print("Test.")
如果你想简单地传递所有的参数,你可以这样做:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
"""A test!"""
print("Test.")
这使用 *
and **
syntax to get all the arguments and keyword arguments and then uses super
调用带有这些参数的超级 类 __init__
。
备选方案(更多工作)是:
def __init__(self, image, x, y):
super().__init__(image=image, x=x, y=y)
"""A test!"""
print("Test.")
我正在学习这本书,'Python Programming For The Absolute Beginner' 并决定通过制作自己的游戏来测试我的一些技能。游戏基本上是 "don't get hit by the flying spikes",我遇到了一个问题。当 运行 它与此代码:
class Player(games.Sprite):
"""The player that must dodge the spikes."""
def update(self):
"""Move to the mouse."""
self.x = games.mouse.x
self.y = games.mouse.y
self.check_collide()
def check_collide(self):
"""Check for a collision with the spikes."""
for spike in self.overlapping_sprites:
spike.handle_collide()
def main():
pig_image = games.load_image("Mr_Pig.png")
the_pig = Player(image = pig_image,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(the_pig)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main()
我没问题。但是当我想使用'init'时,就像在这段代码中:
class Player(games.Sprite):
"""The player that must dodge the spikes."""
def update(self):
"""Move to the mouse."""
self.x = games.mouse.x
self.y = games.mouse.y
self.check_collide()
def check_collide(self):
"""Check for a collision with the spikes."""
for spike in self.overlapping_sprites:
spike.handle_collide()
def __init__(self):
"""A test!"""
print("Test.")
def main():
pig_image = games.load_image("Mr_Pig.png")
the_pig = Player(image = pig_image,
x = games.mouse.x,
y = games.mouse.y)
games.screen.add(the_pig)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main()
我在 运行 游戏时遇到此错误:
File "run.py", line 85, in main
y = games.mouse.y)
TypeError: __init__() got an unexpected keyword argument 'y'.
这一行:
the_pig = Player(image = pig_image,
x = games.mouse.x,
y = games.mouse.y)
(有点)等同于:
the_pig = Player.__init__(image = pig_image,
x = games.mouse.x,
y = games.mouse.y)
这意味着您的 __init__
应该接受参数 image
、x
和 y
,但您将其定义为:
def __init__(self):
"""A test!"""
print("Test.")
如果你想简单地传递所有的参数,你可以这样做:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
"""A test!"""
print("Test.")
这使用 *
and **
syntax to get all the arguments and keyword arguments and then uses super
调用带有这些参数的超级 类 __init__
。
备选方案(更多工作)是:
def __init__(self, image, x, y):
super().__init__(image=image, x=x, y=y)
"""A test!"""
print("Test.")