[Tutor] call a def/class by reference

bob bgailer at alum.rpi.edu
Thu Sep 29 03:31:36 CEST 2005


At 04:29 PM 9/28/2005, DS wrote:
>What I'm hoping to avoid is an
>explicit reference to any of the called functions within the program.
>By doing it that way, it would avoid a maintenance problem of having to
>remember to put a reference for every new function in the calling program.

Try this - a class in which you define all the functions. The __init__ 
method builds the dictionary.

 >>> class X:
...     funcs = {}
...     def y(self):
...             pass
...     def __init__(self):
...             for itemname in dir(self):
...                     if not itemname.startswith('__'):
...                             item = getattr(self, itemname)
...                             if callable(item):
...                                     self.funcs[itemname] = item
...
 >>> y = X()
 >>> y.funcs
{'y': <bound method x.y of <__main__.x instance at 0x01119828>>}
    



More information about the Tutor mailing list