Python:类型错误:__init__() 正好有 2 个参数(给定 1 个)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30086384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: TypeError: __init__() takes exactly 2 arguments (1 given)
提问by mee
I know this question has been asked several times, but none have managed to provide me with a solution to my issue. I read these:
我知道这个问题已经被问过好几次了,但是没有一个能够为我提供解决我的问题的方法。我读了这些:
__init__() takes exactly 2 arguments (1 given)?
__init__() 正好接受 2 个参数(给定 1 个)?
class __init__() takes exactly 2 arguments (1 given)
类 __init__() 正好接受 2 个参数(给定 1 个)
All I am trying to do is create two classes for a "survival game" much like a very crappy version of minecraft. Bellow is the full code for the two classes:
我想要做的就是为“生存游戏”创建两个类,就像一个非常糟糕的我的世界版本。Bellow 是这两个类的完整代码:
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
And now for the full error:
现在是完整的错误:
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)
I just want to make the classes work.
我只想让课程发挥作用。
采纳答案by Jerry
The code you used is as follows:
您使用的代码如下:
player = Player()
This is an issue since the __init__
must be supplied by one parameter called name
according to your code. Therefore, to solve your issue, just supply a name to the Player constructor and you are all set:
这是一个问题,因为__init__
必须由name
根据您的代码调用的一个参数提供。因此,要解决您的问题,只需为 Player 构造函数提供一个名称即可:
player = Player('sdfasf')
回答by Bhargav Rao
The problem is that the Class Player
's __init__
function accepts a name
argument while you are initializing the Class instance. The first argument, self
is automatically handled when you create a class instance. So you have to change
问题是 ClassPlayer
的__init__
函数name
在您初始化 Class 实例时接受一个参数。第一个参数self
在您创建类实例时自动处理。所以你必须改变
player = Player()
to
到
player = Player('somename')
to get the program up and running.
启动并运行程序。
回答by MattDMo
__init__()
is the function called when the class is instantiated. So, any arguments required by __init__
need to be passed when creating an instance. So, instead of
__init__()
是类被实例化时调用的函数。因此,__init__
创建实例时需要传递所需的任何参数。所以,而不是
player = Player()
use
用
player = Player("George")
The first argument is the implicit self
, which doesn't need to be included when instantiating. name
, however, is required. You were getting the error because you weren't including it.
第一个参数是隐式self
,实例化时不需要包含它。name
,然而,是必需的。您收到错误是因为您没有包含它。
回答by Tenzin
Your code know expects that you input something in __init__
which you don't.
I have made a simple example below that does give you an idea where the error __init__() takes exactly 2 arguments (1 given)
is coming from.
What I did is I made a definition where I give input to useless
.
And I am calling that definition from __init__
.
您的代码知道期望您输入一些__init__
您没有输入的内容。
我在下面做了一个简单的例子,它确实让你知道错误__init__() takes exactly 2 arguments (1 given)
来自哪里。
我所做的是我做了一个定义,在那里我给useless
.
我从__init__
.
Example code:
示例代码:
class HelloWorld():
def __init__(self):
self.useThis(1)
def useThis(self, useless):
self.useless = useless
print(useless)
# Run class
HelloWorld()
If you have a definition like def exampleOne(self)
it doesn't expect any input. It just looks a t itself.
But def exampleTwo(self, hello, world)
expects two inputs.
So to call these two you need:
如果您有这样的定义,def exampleOne(self)
则不需要任何输入。它只是看着自己。
但def exampleTwo(self, hello, world)
需要两个输入。
所以要调用这两个你需要:
self.exampleOne()
self.exampleTwo('input1', 'input2')