Is this a good approach to execute a list of operations on a data structure in Python?
Posted
by Sridhar Iyer
on Stack Overflow
See other posts from Stack Overflow
or by Sridhar Iyer
Published on 2010-04-30T23:51:01Z
Indexed on
2010/04/30
23:57 UTC
Read the original article
Hit count: 176
python
|coding-style
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?
© Stack Overflow or respective owner