How to declare a dictionary with inline function

Do you really need a dictionary, or just getitem access?

If the latter, then use a class:

>>> class Dispatch(object):
...     def funcA(self, *args):
...         print('funcA%r' % (args,))
...     def funcB(self, *args):
...         print('funcB%r' % (args,))
...     def __getitem__(self, name):
...         return getattr(self, name)
... 
>>> d = Dispatch()
>>> 
>>> d['funcA'](1, 2, 3)
funcA(1, 2, 3)

The answer seems to be that there is no way to declare a function inline a dictionary definition in python. Thanks to everyone who took the time to contribute.

Tags:

Python