-4

Here is my code that calls the __init__ of Game_Events class:

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

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

But when ever I run the code I get:

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

How can I fix this?

user3519870
  • 91
  • 2
  • 10
  • 4
    `GE = Game_Events("no")`, you might also be better of giving DoSetup a default value `DoSetup=False` – Padraic Cunningham Jan 12 '16 at 13:19
  • This may provide you with more information on init: http://stackoverflow.com/questions/8609153/why-do-we-use-init-in-python-classes – Panda Jan 12 '16 at 13:46

2 Answers2

4

Game_Events.__init__() is called for you, you don't call it directly. It is called when you create an instance:

Game_Events()  # calls Game_Events.__init__(newly_created_instance)

Because you didn't pass in an argument to that call, things break. Pass in 'no' to that call:

GE = Game_Events('no')  # calls Game_Events.__init__(newly_created_instance, 'no')
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

init functions are called while creating an instance for that class only.

Try this:

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
Rohit Barnwal
  • 482
  • 6
  • 19