Re: Python: A Program I'd Love To Understand

  • From: Q <q@xxxxxxxxxxxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Wed, 22 Jun 2011 15:10:19 -0400

each function returns the next key in the dictionary to call by the main loop. Presumably the last one exits or goes back to the beginning or something because otherwise the loop never exits. Tip: call your argument something other than map, perhaps room_map or something, as map is a builtin function that you're shadowing.



On 6/22/2011 2:56 PM, Homme, James wrote:

Hi,

In the program in question, here is more code with more attempts to understand what's going on.

In the real program is a function call that is physically below the function that it calls.

Here's the function, and call, heavily commented in an attempt to understand what's going on. Please correct and elaborate if necessary.

def runner(map, start):

  # Loop through the dictionary and call each function.

print "Top of runner function." # For understanding what's going on. Remove later

  # next will be the first function of the dictionary.

  next = start

# I think that the condition will be False once we have no more functions to loop through.

  while True:

    print "Top of loop: next is now %s." % next # Remove later.

room = map[next] # Get the name of the function we are going to call further down.

    print "Room is now %s." % room # Remove later.

    print "\n--------"

    next = room() # Call the function and get back what it returns.

    print "Bottom of loop: next is now %s." % next

# Call runner. Give it the dictionary, and which key to start with.

runner(ROOMS, 'princess_lives_here')

Thanks.

Jim

*From:*programmingblind-bounce@xxxxxxxxxxxxx [mailto:programmingblind-bounce@xxxxxxxxxxxxx] *On Behalf Of *Q
*Sent:* Wednesday, June 22, 2011 2:04 PM
*To:* programmingblind@xxxxxxxxxxxxx
*Subject:* Re: Python: A Program I'd Love To Understand

Jim,
Those functions are defined elsewhere in the module, right?
Python supports a computer science concept known as first-class functions:
http://en.wikipedia.org/wiki/First-class_function
If you don't understand, feel free to poke me and I'll explain further.

On 6/22/2011 1:59 PM, Homme, James wrote:

Hi,

I have lots of questions about the current Python exercise I'm working on, so I'll just ask one at a time. Hope you have lots of patience.

First question. I think I should be getting a compiler error but I'm not. I thought I had to put some sort of quotes around this code after the commas. Why is this compiling OK?

ROOMS = { 'death': death,

  'princess_lives_here': princess_lives_here,

  'gold_koi_pond': gold_koi_pond,

  'big_iron_gate': big_iron_gate,

  'bear_with_sword': bear_with_sword

  }

Thanks.

Jim

Jim Homme,

Usability Services,

Phone: 412-544-1810.


This e-mail and any attachments to it are confidential and are intended solely for use of the individual or entity to whom they are addressed. If you have received this e-mail in error, please notify the sender immediately and then delete it. If you are not the intended recipient, you must not keep, use, disclose, copy or distribute this e-mail without the author's prior permission. The views expressed in this e-mail message do not necessarily represent the views of Highmark Inc., its subsidiaries, or affiliates.


Other related posts: