understanding classes in python 2 [learn python th

2019-06-12 07:00发布

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'):

http://pastebin.com/zhntxFxS

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?

3条回答
Explosion°爆炸
2楼-- · 2019-06-12 07:46

The return is part of the method named enter of the classes CentralCorridor or Death. So this method will just return a value when it is called and if the condition is met:

>>> corridor = CentralCorridor(scene_map)
>>> result = corridor.enter() # Here the program waits for you to write something
>tell a joke
>>> print result
laser_weapon_armory

This mechanism is internally used by the method play of the Engine 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 are dodge and tell a joke. Any other answer will leave you in the corridor.

查看更多
干净又极端
3楼-- · 2019-06-12 07:48

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 the Engine() class. If you see in your play function in Engine, it calls opening_scene(), which seems to be only limited to the Map() class. That's why you have passed the instance of Map to Engine.

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))

查看更多
ら.Afraid
4楼-- · 2019-06-12 07:58

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. When next_scene is called the string passed in is used as a key into the dictionary called scenes. The values in scenes 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 calls enter_scene on whatever scene Map gives it. The scene has a string identifying the next scene, which is then retrieved, entered etc. This loop goes on until the scene Death is entered, at which point the call to exit ends the program.

Hope this makes some kind of sense, have fun with Python!

查看更多
登录 后发表回答