dynamically generating function assignent

Xtian Muirhead xtian_the_great at hotmail.com
Mon Nov 1 20:28:51 EST 1999

Xtian Muirhead wrote...
> Another way would be to load all the functions into a dictionary.
> e.g:
> def func_1():
>     <blah>
> def parse_func_1():
>     <blah>
> ...
> functions = {'1':(func_1,parse_func_1),
>              'a':(func_a, parse_func_a),
>                 ...}
> Then you could look up call1 and call2:
> call1, call2 = functions[valueFromFile]
> call1()
> call2()
> This could be easier to catch cases where the value from the file
> doesn't match any of the functions defined. (And I prefer this kind
> of static lookup to a dynamic lookup. That's just a bias, I guess.
> Suffice it to say that both of these ways are nicer than a whole heap
> of elifs. (I guess if you had hundreds of these functions, you wouldn't
> want to have to type out all of them in a dictionary (with the
> attendant likelihood of making an error).))

Or maybe I'm very silly, and those functions would all be entered
automatically in globals() - then your dictionary lookup could be
from that (making sure that the resultant references did point at
callable objects).
(How do you cancel a usenet message when you realise you'd be
better off just lurking for a bit longer?)


xtian at regurgitator.zzn.com

"Probably the earliest flyswatters were nothing more than some
sort of striking surface attached to the end of a long stick."

More information about the Python-list mailing list