Question about using python as a scripting language

Jordan Greenberg greenbergj at wit.edu
Mon Aug 7 04:46:34 EDT 2006


Terry Reedy wrote:
> "heavydada" <jlara_garduno at hotmail.com> wrote in message 
<snip>
>> I just need some way of
>> being able to read from the file what function the program needs to
>> call next. Any help is appreciated.
> 
> Suppose you have a file actions.py with some action functions:
> def hop(self): ...
> def skip(self): ...
> def jump(self)
<more snipping>
> Terry Jan Reedy

Another convenient way if, for some reason, you're not creating objects
for your creatures would be using a dictionary to look up functions, like:

def hop(x):
	return x+1
def skip(x):
	return x+2
def jump(x):
	return x+3

actionlookup={"hop": hop, "skip": skip, "jump": jump}
action=actionlookup["hop"]
action() #this will call hop()

Please note that I'm only including this for completeness, for any
larger project (or medium sized, or anything more then a few lines,
really) this gets really unwieldy really quickly (imagine if you had
thousands of functions! Madness!) Terry's suggestion is a much better
solution then this. If this looks easier, consider changing the rest of
your program before kludging something hideous like this together.

Good luck,
-Jordan Greenberg

-- 
Posted via a free Usenet account from http://www.teranews.com




More information about the Python-list mailing list