[Tutor] How to put my functions in an array

Alan Gauld alan.gauld at freenet.co.uk
Thu Dec 30 09:08:05 CET 2004

> Thanks alot Jeff. This worked for me:
> formhandlers={}
> for verb,verb_desc in PROVISION_ACTIONS:
> try:
> formhandlers[verb]=globals()[verb]
> except KeyError:
> pass

I'm slightly confused about why you need to do this?
You create a list of names (PROVISION_ACTIONS), then 
you add the corresponding functions to a dictionary 
by looking the names up in the globals dictionary. 
But since uyou know the names of the functions why 
not just add them to the actions list in the first place?

PROVISION_ACTIONS = [('foo',foo),('bar',bar)]

formhandlers = {}
for name,action in PROVISION_ACTIONS:
   formhandlers[name] = action

Or even easier do it all in one step:

formhandlers = { 'foo':foo,

Why do you need the globals lookup when you know the 
names of the functions already?

I'm missing something...

Alan G.

More information about the Tutor mailing list