Is this a good approach to execute a list of operations on a data structure in Python?
Question: I have a dictionary of data, the key is the file name and the value is another
dictionary of its attribute values. Now I'd like to pass this data structure
to various functions, each of which runs some test on the attribute and
returns True/False.

One approach would be to call each function one by one explicitly from the
main code. However I can do something like this:

    
    
    #MYmodule.py
    class Mymodule:
      def MYfunc1(self):
      ...
      def MYfunc2(self):
      ...
    
    #main.py
    import Mymodule
    ...
    #fill the data structure
    ...
    #Now call all the functions in Mymodule one by one
    for funcs in dir(Mymodule):
       if funcs[:2]=='MY':
          result=Mymodule.__dict__.get(funcs)(dataStructure)
    

The advantage of this approach is that implementation of main class needn't
change when I add more logic/tests to MYmodule.

Is this a good way to solve the problem at hand? Are there better alternatives
to this solution?


Answer: I'd say a better and much more Pythonic approach would be to define a
[decorator](http://www.artima.com/weblogs/viewpost.jsp?thread=240808) to
indicate which functions you want to use:

    
    
    class MyFunc(object):
        funcs = []
        def __init__(self, func):
            self.funcs.append(func)
    
    @MyFunc
    def foo():
        return 5
    
    @MyFunc
    def bar():
        return 10
    
    def quux():
        # Not decorated, so will not be in MyFunc
        return 20
    
    for func in MyFunc.funcs:
        print func()
    

Output:

    
    
    5
    10
    

Essentially you're performing the same logic: taking only functions who were
defined in a particular manner and applying them to a specific set of data.

