【问题标题】:Python: TypeError: __init__() takes exactly 2 arguments (1 given)Python:TypeError:__init__() 正好需要 2 个参数(1 个给定)
【发布时间】:2015-07-17 03:49:26
【问题描述】:

我知道这个问题已经被问过好几次了,但没有人能够为我的问题提供解决方案。我读了这些:

__init__() takes exactly 2 arguments (1 given)?

class __init__() takes exactly 2 arguments (1 given)

我要做的就是为“生存游戏”创建两个类,就像非常糟糕的 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)

我只想让课程正常工作。

【问题讨论】:

  • Player 接受name 参数,但您什么都不传递。
  • 没错,您传递了隐含的self 参数,但没有传递name 参数。
  • 稍后在文本文件中我实际上做了 x = Player("string") 然后从那里继续。我只是忘了在这里包含那一点
  • 你链接的两个问题完美地解释了这个问题。不知道你是怎么从那里弄出来的......

标签: python class typeerror


【解决方案1】:

你使用的代码如下:

player = Player()

这是一个问题,因为根据您的代码,__init__ 必须由一个名为 name 的参数提供。因此,要解决您的问题,只需为 Player 构造函数提供一个名称即可:

player = Player('sdfasf')

【讨论】:

  • 为什么投反对票?问题是让两个类正常工作,这是最简单的答案。
  • 我也不同意投反对票,但是... 今后 1. 解释为什么他应该这样做 2. 回答时不要使用短信语言 3. 有一些有意义的名字。纠正这 3 个,downvoter 肯定会删除他的 downvote
  • 当您将鼠标悬停在否决按钮上时,弹出文本显示“此答案没有用。”一些可能的原因:1)您使用的是 txt-speak,它有些人觉得非常烦人和无知,2)你什么都不解释,只是说“这样做,它会起作用”,3)它是在其他两个答案之后发布的,两者都是其中解释了错误的原因以及如何修复它。
  • 好的。我编辑了我的答案,使其更具描述性。感谢您的反馈。
【解决方案2】:

问题在于,当您初始化 Class 实例时,Class Player__init__ 函数接受 name 参数。第一个参数self 在您创建类实例时自动处理。所以你必须改变

player = Player()

player = Player('somename')

让程序启动并运行。

【讨论】:

    【解决方案3】:

    __init__() 是类实例化时调用的函数。因此,__init__ 所需的任何参数都需要在创建实例时传递。所以,而不是

    player = Player()
    

    使用

    player = Player("George")
    

    第一个参数是隐含的self,实例化时不需要包含。但是,name 是必需的。您收到错误是因为您没有包含它。

    【讨论】:

    • 而不是在 Food 类中键入 Player("George"),我怎样才能使 Player(name) 工作并获取我在运行实际 Player 类时输入的 name 值有论据?
    • @mee 我也不完全理解你想要做什么。你想创建一个可以在所有类中使用的名为“name”的变量吗?如果是,则将变量设为全局变量。然后每个类/定义都可以使用它。或者您可以使用继承将变量传递给另一个类。
    • 在完整的文件中,最后我有这些行: x = Player("string") print x.name print x.health x.eat('apple') print x.health 是有一种方法可以让我在 Food 类中初始化 Player,而不必再次编写“string”,而是使用 x = Player(“string”) 的值?我希望它现在更清楚
    【解决方案4】:

    您的代码知道您在__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) 的定义,则不需要任何输入。它本身看起来就是个 t。
    但是def exampleTwo(self, hello, world) 需要两个输入。
    所以要调用这两个你需要:

    self.exampleOne()
    self.exampleTwo('input1', 'input2')
    

    【讨论】:

    • 这如何回答这个问题?
    • 它解释了错误“__init__() 恰好需要 2 个参数(1 个给定)?”来自。以及为什么会出现该错误。这是主题起始帖子被编辑之前的最初问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    相关资源
    最近更新 更多