Function lookup using a directory.

Nick Perkins nperkins7 at home.com
Mon Jun 18 22:59:59 EDT 2001


...another thing to keep in mind is that you could also store
functions as the values of the dictionary, just as easily as class
instances.

Maybe you prefer class instances, but your use of a 'run' method might
indicate that what you really want to keep in the dictionary is functions.
(your subject line even says so)


eg.

def Help0(args):
    blah blah blah
...

commands = {0:Help0, ...

..then you can retrieve the function later..

fn = commands[n]

..and call the function by simply using..

fn()

( no run() method necessary )

In Java, you would need to define a class with a 'run' method,
but in Python, a function can be passed around like any other object.






More information about the Python-list mailing list