Complete novice programmer here. I've been going through Zed's Learn Python the Hard Way book for the past few weeks to cover the basics of programming, and I've found myself at a standstill. I've more or less been following everything he's been throwing at me, but once I found myself at exercise 43, Zed seems to have thrown me overboard. He outlined, and I think I understand, the basics of class creation along with accessing the variables and functions within them and a bit on class inheritance. Once I got to exercise 43, Zed seemed to have forgotten to explain a very large chunk on how classes can interact with each other, specifically using the return function (or maybe I'm the one who has not been paying attention well enough). I skimmed through the next chapter and he didn't seem to explain it at all, so I came here.
I want to understand how exactly the different 'scenes' transition in the following lines of code (abridged, I took out a lot of the 'fluff'):
I have a few big questions: How does the return function work inside a class?
What does the return function do in the CentralCorridor() class (or the Map() and Engine() classes)?
How do the Map() and Engine() classes work off each other to change scenes?
and slightly less related: Will the following few chapters of Zed's book be beneficial to me as a novice or should I look elsewhere for python help if I'm struggling here?
The
return
is part of the method namedenter
of the classesCentralCorridor
orDeath
. So this method will just return a value when it is called and if the condition is met:This mechanism is internally used by the method
play
of theEngine
class.The change of map is done by the user, when the program ask him to enter a value (
raw_input
). The name of the next map depends on the value and of the current map. For instance, if the map is the central corridor, the possible values to change the map aredodge
andtell a joke
. Any other answer will leave you in the corridor.The
return
statement isn't necessarily being used in a class, but more specifically, in a function which happens to be in a class. So when you call the function in the class, that will be returned. There's nothing different to it than a normal function.You create an instance of the
Map()
class to use in theEngine()
class. If you see in yourplay
function inEngine
, it callsopening_scene()
, which seems to be only limited to theMap()
class. That's why you have passed the instance ofMap
toEngine
.I did LPTHW a while ago, so I don't remember the later chapters. If you feel you aren't learning from one tutorial, perhaps try another (I actually went from Codecademy to LPTHW because Codecademy wasn't that great at teaching classes (or maybe it was just me :p))
In the code on pastebin you have
return
used inside methods of classes, not inside classes directly. In a method (just a function associated with an object, really)return
works just like it does in a regular function - it passes back some piece of data (or nothing) to whatever called it and returns control to the caller to so the calling code can proceed.The Map object manages the different scene objects, and provides a method,
next_scene
that can be used to retrieve a scene object given a name. Whennext_scene
is called the string passed in is used as a key into the dictionary calledscenes
. The values inscenes
are objects representing the different scenes. Once the correct scene object has been found in the dictionary,Map
returns it.The Engine class has a single method of note,
play
. This essentially is an infinite loop, on each pass it uses the Map object to retrieve a scene and then callsenter_scene
on whatever sceneMap
gives it. The scene has a string identifying the next scene, which is then retrieved, entered etc. This loop goes on until the sceneDeath
is entered, at which point the call to exit ends the program.Hope this makes some kind of sense, have fun with Python!