理解python中的类2 [学习python的困难的方式练习43] [英] understanding classes in python 2 [learn python the hard way exercise 43]

查看:99
本文介绍了理解python中的类2 [学习python的困难的方式练习43]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里完成新手程序员。我过去几个星期经历了Zed的学习Python困难的方式书,以覆盖编程的基础知识,我发现自己处于停滞状态。我已经或多或少地跟着他一直扔在我身上的一切,但一旦我发现自己在运动43,Zed似乎把我抛在了我。他概述了,我想我理解,类创建的基础,以及访问它们中的变量和函数,以及类继承。一旦我练习43,Zed似乎忘了解释一个非常大的块,如何类可以互相交互,特别是使用 return 函数(或者我是一个没有一直注意得很好)。我仔细看了下一章,他似乎没有解释它,所以我来这里。

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.

我想了解不同的场景在下面几行代码中(简化了,我取出了很多'fluff'):

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?

返回函数在CentralCorridor()类(或Map()和Engine()类)中执行什么操作?

What does the return function do in the CentralCorridor() class (or the Map() and Engine() classes)?

Map()和Engine()类如何相互协作来改变场景?

How do the Map() and Engine() classes work off each other to change scenes?


如果我在这里挣扎,那么Zed的这本书的下面几章会对我有益吗?或者我应该去别的地方寻找python帮助吗?

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?

推荐答案

在pastebin的代码中,你有'返回'在类的方法中使用,而不是直接在类中。在一个方法(只是一个与对象关联的函数,真的)'return'的工作就像在一个普通的函数 - 它传回一些数据(或没有什么)到任何调用它,并返回控制给调用者调用代码可以继续。

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.

Map对象管理不同的场景对象,并提供一个方法next_scene,用于检索给定名称的场景对象。当调用'next_metgod'时,传入的字符串被用作称为场景的字典中的键。 场景中的值是表示不同场景的对象。一旦在字典中找到正确的场景对象,Map将返回它。

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_metgod' 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.

Engine类有一个简单的方法,play。这本质上是一个无限循环,在每次通过它使用Map对象来检索场景,然后调用'enter_scene'在任何场景Map给它。场景有一个字符串标识下一个场景,然后检索,输入等。Thos循环继续,直到进入场景'死亡',此时退出调用结束程序。

The Engine class has a singe 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. Thos loop goes on until the scene 'Death' is entered, at which point the call to exit ends the program.

希望这有某种意义,使用Python很有趣!

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

这篇关于理解python中的类2 [学习python的困难的方式练习43]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆