__Init__ goal

I have done some reading and cannot understand it as fully as I would like. I do a little "choose your adventure" from the LPTHW tutorial, here is the full script: http://codepad.org/YWVUlHnU

I do not understand the following:

class Game(object): def __init__(self, start): self.quips = [ "You died. Please try again.", "You lost, better luck next time.", "Things didn't work out well. You'll need to start over." "You might need to improve your skills. Try again." ] self.start = start 

I realized that we are creating a class, but why define __init__ ? Later I make stuff like print self.quis[randint(0, len(self.quips)-1)] , which prints one of four lines in quips , but why don't I just create a function called quips or something else ?

+4
source share
3 answers

When you call Game("central_corridor") , a new object is created, and the Game.__init__() method is called with this new object as the first argument ( self ) and "central_corridor" as the second argument. Since you wrote a_game = Game(...) , you assigned a_game to reference this new object.

This graphic can simplify the process:

Python object creation

Note. The __new__ method __new__ provided by Python. It creates a new class object specified as the first argument. The __new__ built-in method __new__ nothing with the rest of the arguments. If you need to, you can override the __new__ method and use other arguments.

The practical reason __init__() exists in your program is to set the start attribute on the Game instance you created (the one you call a_game ), so the first call to a_game.play() starts where you want.

You are right about quips . There is no reason to set quips in __init__() . You can simply make it an attribute of the class:

 class Game(object): quips = ["You died. Please try again.", "You lost, better luck next time.", "Things didn't work out well. You'll need to start over." "You might need to improve your skills. Try again." ] def __init__(self, start): self.start = start 
+15
source

More broadly, __init__ and the lesser-known and often more dangerous __new__ methods of the python class must initialize the state of your object.

For your specific object, this is really not necessary, and I believe that there are quite a few very simple data storage classes that you could imagine, since they are not needed for initialization, but the reality of deeper programming with python is that practically an increasingly complex class must be initialized to its base state.

In addition, the big reason is to make your class multifunctional:

 class Foo(object): def __init__(self, word_of_power="Ni"): self.fact = "We are the knights who say %!" % word_of_power >>> f = Foo() >>> w = Foo("No!") >>> print f.fact "We are the knights who say Ni!" >>> print w.fact "We are the knights who say No!" 
+5
source

__init__ works here as a constructor. Therefore, when a game instance is created, the __init__ method is called. If you define a different quips configuration method, you will need to explicitly call it.

+1
source

Source: https://habr.com/ru/post/1396922/


All Articles