Exercise 43, some explanation needed

So I don’t understand the use of self at all anywhere in the code for this exercise. For example in

class Engine(object):

def init(self, scene_map):
self.scene_map = scene_map

def play(self):
current_scene = self.scene_map.opening_scene()

  while True:
  	print "\n---------"
  	next_scene_name = current_scene.enter()
  	current_scene = self.scene_map.next_scene(next_scene_name)

Why do we need self in “def init(self, scene_map)”?
Why do we put self in “self.scene_map = scene_map”?
In def play(self):? Why not just have it def play()?

Hi @Trezor.

When you have a:

def __init__(self, scene_map):
    self.scene_map = scene_map

you can pass in an argument when you initialize the class.
That is whats happening at the end of the file.

a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()

First a_map become a instance of Map class with ‘central_corridor’ as argument
Then a_game become a instance of Engine class with a_map as argument.
At last the instance (a_game) is connected with the attribute (play)

a_game.play()

In the class method play you can see a while loop.
As long as the conditions are True the game will go on.
It starts with:

‘central_corridor’

If you dont get killed it continues with:

laser_weapon_armory’
the_bridge’
escape_pod’

It is death and finished that will change the conditions to False and it stops the loop.

I realise now (after a cup of coffe) that this was perhaps not answer to your question.
I hope it is somewhat useful anyway.

1 Like

Here is a link to Medium which have an explanation of self

I hope this helps a little more

My six year old daugther use to tell me when I have no answer to all of her questions:
Google it daddy. Goggle it!
:wink:

1 Like

Thanks. Congratulations on having a daughter. She seems very bright.

I did google it but couldn’t really understand the explanations.

Hi @Trezor

Did you edited your post earlier today?

I think I saw more of your code before.
If so I think there was a missing return statement after the while loop.
Or in the loop. I am still at work, so I cannot give you more than this at the moment…

It is

return current_scene.enter()

Or just

return current_scene()

1 Like

Thanks, yes I did but then I figured it out so I removed it. I have a hard time understanding the Engine class, I have read the explanation in the book but still don’t get it.

Let’s start with only this piece of code:

def __init__(self, scene_map):
		self.scene_map = scene_map

Okay, so we define a function. I don’t understand init fully after googling but I am content with knowing you need it if you define a function inside a class. Then we have self which is like a placeholder which is also just needed otherwhise it doesn’t work.

Why do we set self.scene_map to scene_map? What happens when we do that?

Hello @Trezor

I will try to give a little explanation. I hope it will help you a little bit

When you put in a

class Thing(object):
    def __init__(self, something):
        self.something = something

into a class you can the pass in that “something” in as argument inside the brackets when you instantiate the class.

x = Thing('Tree')

I dont think you can do this without the def init method.

Have a look at this guy at Youtube
He explain classes and objects thorough.

1 Like

So, I think the first problem is you have to figure out what scene_map is and where it comes from. Try this out:

  1. Get yourself a piece of paper and a pencil or pen.
  2. Now, find every variable in the script and write down where it’s created. You’d look for where it’s set = to something, and “where” should be something like the line number, then the code its in. For example: scene_map | line 25 | script. If something was in a class function it’d be another_var | line 300 | MyClass.thefunc.
  3. Next, add to that line what class or type that variable is. So, you’d say scene_map | line 25 | script | class Map.
  4. Now that you know where every variable is created, you can go back to studying your code. When you hit a spot you don’t understand, the first thing to ask is “where are these variables coming from AND what are they.” In this case, you say “scene_map is a Map class coming from line X in the main script.”
  5. Finally, say what you’re doing to that variable. In this case you say, “I’m assigning it to the self.scene _map variable.”
  6. Ok, so you have list of variables and where they come from right? Now, later in that class you start using self.scene_map to do stuff. You have your list of every variable right? So you can then go look in your list and see, “Oh, self.scene_map comes from scene_map that was defined at the bottom of the script, so that means self.scene_map is a Map.”

Now, why do you need self? Imagine I have this code:

class Apple():
    def eat(self):
        print("You ate it.")

x = Apple()
y = Apple()
j = Apple()

I just made 3 apples, x, y, and j. Each of those are completely separate things, and the Apple() call simply is a way to craft those using the class Apple “template”. After this they run on their own and mostly don’t interact. Ok, so what if I did this:

x.eat()

How does the Apple.eat function know that it’s working on x and not y or j? Try to answer that and it should help you figure out what self does. Another clue is that self == x. But if you were to do y.eat() then self == y.

1 Like