Python: TypeError: __init__() takes exactly 2 arguments (1 given)
Python: TypeError: __init__() takes exactly 2 arguments (1 given)
我知道这个问题已经被问过好几次了,但是 none 已经设法为我提供了解决问题的方法。我读了这些:
__init__() takes exactly 2 arguments (1 given)?
class __init__() takes exactly 2 arguments (1 given)
我想做的就是为一个 "survival game" 创建两个 类,就像一个非常糟糕的 minecraft 版本。下面是两个 类:
的完整代码
class Player:
'''
Actions directly relating to the player/character.
'''
def __init__(self, name):
self.name = name
self.health = 10
self.shelter = False
def eat(self, food):
self.food = Food
if (food == 'apple'):
Food().apple()
elif (food == 'pork'):
Food().pork()
elif (food == 'beef'):
Food().beef()
elif (food == 'stew'):
Food().stew()
class Food:
'''
Available foods and their properties.
'''
player = Player()
def __init__(self):
useless = 1
Amount.apple = 0
Amount.pork = 0
Amount.beef = 0
Amount.stew = 0
class Amount:
def apple(self):
player.health += 10
def pork(self):
player.health += 20
def beef(self):
player.health += 30
def stew(self):
player.health += 25
现在完整的错误:
Traceback (most recent call last):
File "/home/promitheas/Desktop/programming/python/pygame/Survive/survive_classe s.py", line 26, in <module>
class Food:
File "/home/promitheas/Desktop/programming/python/pygame/Survive/survive_classe s.py", line 30, in Food
player = Player()
TypeError: __init__() takes exactly 2 arguments (1 given)
我只是想让 类 正常工作。
问题是 Class Player
的 __init__
函数在您初始化 Class 实例时接受了 name
参数。第一个参数 self
在您创建 class 实例时自动处理。所以你必须改变
player = Player()
至
player = Player('somename')
启动程序并运行。
__init__()
是实例化class时调用的函数。因此,创建实例时需要传递 __init__
所需的任何参数。所以,而不是
player = Player()
使用
player = Player("George")
第一个参数是隐式的 self
,实例化时不需要包含它。但是,name
是必需的。您收到错误是因为您没有包含它。
您使用的代码如下:
player = Player()
这是一个问题,因为根据您的代码,__init__
必须由一个名为 name
的参数提供。因此,要解决您的问题,只需为 Player 构造函数提供一个名称即可:
player = Player('sdfasf')
您的代码知道您在 __init__
中输入了一些您没有输入的内容。
我在下面做了一个简单的例子,它确实让你知道错误 __init__() takes exactly 2 arguments (1 given)
是从哪里来的。
我所做的是在我为 useless
提供输入的地方做了一个定义。
我从 __init__
调用该定义。
示例代码:
class HelloWorld():
def __init__(self):
self.useThis(1)
def useThis(self, useless):
self.useless = useless
print(useless)
# Run class
HelloWorld()
如果您有一个像 def exampleOne(self)
这样的定义,它不需要任何输入。它只是看着自己。
但是 def exampleTwo(self, hello, world)
需要两个输入。
所以要调用这两个你需要:
self.exampleOne()
self.exampleTwo('input1', 'input2')
我知道这个问题已经被问过好几次了,但是 none 已经设法为我提供了解决问题的方法。我读了这些:
__init__() takes exactly 2 arguments (1 given)?
class __init__() takes exactly 2 arguments (1 given)
我想做的就是为一个 "survival game" 创建两个 类,就像一个非常糟糕的 minecraft 版本。下面是两个 类:
的完整代码class Player:
'''
Actions directly relating to the player/character.
'''
def __init__(self, name):
self.name = name
self.health = 10
self.shelter = False
def eat(self, food):
self.food = Food
if (food == 'apple'):
Food().apple()
elif (food == 'pork'):
Food().pork()
elif (food == 'beef'):
Food().beef()
elif (food == 'stew'):
Food().stew()
class Food:
'''
Available foods and their properties.
'''
player = Player()
def __init__(self):
useless = 1
Amount.apple = 0
Amount.pork = 0
Amount.beef = 0
Amount.stew = 0
class Amount:
def apple(self):
player.health += 10
def pork(self):
player.health += 20
def beef(self):
player.health += 30
def stew(self):
player.health += 25
现在完整的错误:
Traceback (most recent call last):
File "/home/promitheas/Desktop/programming/python/pygame/Survive/survive_classe s.py", line 26, in <module>
class Food:
File "/home/promitheas/Desktop/programming/python/pygame/Survive/survive_classe s.py", line 30, in Food
player = Player()
TypeError: __init__() takes exactly 2 arguments (1 given)
我只是想让 类 正常工作。
问题是 Class Player
的 __init__
函数在您初始化 Class 实例时接受了 name
参数。第一个参数 self
在您创建 class 实例时自动处理。所以你必须改变
player = Player()
至
player = Player('somename')
启动程序并运行。
__init__()
是实例化class时调用的函数。因此,创建实例时需要传递 __init__
所需的任何参数。所以,而不是
player = Player()
使用
player = Player("George")
第一个参数是隐式的 self
,实例化时不需要包含它。但是,name
是必需的。您收到错误是因为您没有包含它。
您使用的代码如下:
player = Player()
这是一个问题,因为根据您的代码,__init__
必须由一个名为 name
的参数提供。因此,要解决您的问题,只需为 Player 构造函数提供一个名称即可:
player = Player('sdfasf')
您的代码知道您在 __init__
中输入了一些您没有输入的内容。
我在下面做了一个简单的例子,它确实让你知道错误 __init__() takes exactly 2 arguments (1 given)
是从哪里来的。
我所做的是在我为 useless
提供输入的地方做了一个定义。
我从 __init__
调用该定义。
示例代码:
class HelloWorld():
def __init__(self):
self.useThis(1)
def useThis(self, useless):
self.useless = useless
print(useless)
# Run class
HelloWorld()
如果您有一个像 def exampleOne(self)
这样的定义,它不需要任何输入。它只是看着自己。
但是 def exampleTwo(self, hello, world)
需要两个输入。
所以要调用这两个你需要:
self.exampleOne()
self.exampleTwo('input1', 'input2')