无法调用Python函数

时间:2016-01-12 13:18:11

标签: python function

以下是我调用__init__Game_Events的代码:

class Game_Events(object):
    def __init__(self, DoSetup):
        if DoSetup == "yes":
            print "Done"

GE = Game_Events()
GE.__init__("no")

但是当我运行代码时,我得到了:

TypeError: __init__() takes exactly 2 arguments (1 given)

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:4)

Game_Events() # calls Game_Events.__init__(newly_created_instance) 被称为,您不能直接调用它。在创建实例时调用它:

'no'

因为你没有传递给那个电话的参数,所以事情就会破裂。将GE = Game_Events('no') # calls Game_Events.__init__(newly_created_instance, 'no') 传递给该电话:

sudo -s  // use as admin

#public key
wget -O - http://packages.elasticsearch.org/GPG-KEY-elasticsearch | apt-key add -

# edit /etc/apt/sources.list and add
deb http://packages.elasticsearch.org/elasticsearch/1.0/debian stable main
deb http://packages.elasticsearch.org/logstash/1.3/debian stable main

sudo apt-get update
sudo apt-get install elasticsearch

答案 1 :(得分:0)

仅在为该类创建实例时调用

init 函数。

试试这个:

class Game_Events(object):
    def __init__(self, DoSetup):
        if DoSetup == "yes":
            print "Done"

GE = Game_Events("no")

GE = Game_Events("yes") # output will be yes