开发者

Python question on parse routines with dicts, while loops, and assignments

开发者 https://www.devze.com 2023-02-13 20:10 出处:网络
I have some code from a much-maligned Python resource, if you ask SO, but I am trying to divine Python’s parse routine from it, regardless its didactic merits.

I have some code from a much-maligned Python resource, if you ask SO, but I am trying to divine Python’s parse routine from it, regardless its didactic merits.

def princess_lives_here():
    #<some messages and conditionals to keep you going in the text-based game>
    #<one of the conditionals end in a `return 'death'`, another function in ROOMS>

ROOMS = {
 开发者_开发百科   'death': death,
    'princess_lives_here': princess_lives_here,
}

def runner (map, start):
    next = start

    while True:
        room = map[next]
        print "\n--------"
        next = room()

runner(ROOMS, 'princess_lives_here')
  1. What I am not sure about is how princess_lives_here is ever run in the while loop. I can only assume that it is run is the last line, but what looks like an assignment to me must be an execution of room(), i.e. princess_lives_here. Why, when and how is this execution performed?

  2. My second question is how, when, and why the while loop is run again, when the parsing routines hits return 'death'.

I’ve created a gist with the entire code, in case you want the greater picture. It just takes up a lot of lines of code: https://gist.github.com/848228.


After looking at the original code will analyze it for you:

1) The program enters at the point:

runner(ROOMS, 'princess_lives_here')

2) runner function gets a map (a list of available rooms where we can go) and a start point at the map. So, the previous call uses the map defined in the ROOMS dictionary and sets us in the princess_live_here room.

3) The runner loop, gets the actual room dictionary value, so, we get a reference for a function called princess_live_here that is defined at top of the file, prints some output and calls that function using the reference.

4) At princess_lives_here we can input some text, that text will decide what to do next, returning the next room we will go. For example, if we write: "make her eat it", the function will return the key for the 'gold_koi_pond' room, and the loop will then setup that room as the actual room for us, and get again the dictionary value for that room and call it.

So, answering you more focused way:

a) The execution is that I've described above this lines b) When any function returns 'death' the execution is the same as above, but the death function will print a random quote and exit the program.

Just that.


First thing to mention, is that next is a built-in function in Python and as soon as you assign it to anything else you are loosing it's reference for futures accesses. Therefore:

>>> next
<built-in function next>
>>> next = 'a'
>>> next
'a'
>>> 

It's considered a bad practice to override built-in functions. As soon as you assign next to start (first line of runner function) the reference to next built-in is lost in the scope of that function.

More specifically on the questions you posted:

1) Think of functions as 'objects' that can be assigned to variables and that by postfixing () you make a call to them. It's also similar to the concept of pointer to a function in C. In your design, the pointers to functions reside in the dictionary ROOMS, so that when you access as:

room = ROOMS["princess_lives_here"]

room will point to the function princess_lives_here and room() will perform a call to that function. If this function returns a string that is a key on the ROOM dictionary then the next iteration of the loop will do exactly the same.

2) Looking at the code you posted it. if "death" is returned then next will hold that string value, which will be the lookup key for the dictionary ROOM (1st line of the loop), which will make room to hold a pointer to the function death(2nd line of the loop). And the last line of the loop will perform a call to the function that is pointed by room, which is death in this case. Since this function performs exit then the program will terminate. No more iterations will happen.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号