Calling Methods Line by Line

I have the following class.

func_list= ["function1", "function2", "function3"] class doit(object): def __init__(self): for item in func_list: if item == "function1": self.function1() elif item == "function2": self.function2() elif item == "function3": self.function3() def function1(self): #do this pass def function2(self): #do that pass def function3(self): pass 

If an instance of this class is created, it iterates over the list of strings and call methods depending on the actual string. The lines in the list have the names of the corresponding methods.

How can I make it more elegant? I do not want to add another elif -path for each "function" that I am adding to the list.

+6
source share
5 answers
 func_list= ["function1", "function2", "function3"] class doit(object): def __init__(self): for item in func_list: getattr(self, item)() def function1(self): print "f1" def function2(self): print "f2" def function3(self): print "f3" >>> doit() f1 f2 f3 

For private functions:

 for item in func_list: if item.startswith('__'): getattr(self, '_' + self.__class__.__name__+ item)() else: getattr(self, item)() 

.

 getattr(object, name[, default]) 

Gets the value of the named attribute of the object. name must be a string. If the string is the name of one of the attributes of the objects, the result will be the value of this attribute. For example, getattr (x, 'foobar') is equivalent to x.foobar. If the named attribute does not exist, the default value is returned, if provided, otherwise an AttributeError will be raised.

http://docs.python.org/library/functions.html#getattr

+10
source

This is usually solved with a dictionary search, because functions are first-class data types in Python. For instance:

 # map string names to callable functions dispatch = {'func1': func1, 'func2': func2, ...} want_to_call = 'func1' dispatch[want_to_call](args) 
+2
source

Why don't you use lambdas?

For instance,

 def func1(a): return a ** 2 def func2(a, b): return a ** 3 + b dic = {'Hello':lambda x: func1(x), 'World':lambda x,y: func2(x, y)} #Map functions to lambdas print dic['Hello'](3) print dic['World'](2,3) 

Exit, 9 11

OR, you can do it ...

 class funs(): def func1(self, a): return a ** 2 def func2(self, a, b): return a ** 3 + b f = funs() dic = {'Hello': lambda x: f.func1(x), 'World': lambda x,y: f.func2(x,y)} print dic['Hello'](3) print dic['World'](5,4) 

You give 9 129

+1
source
 class doit(object): def __init__(self, func_list): for func in func_list: func(self) #insert other function definitions here func_list = [doit.function1, doit.function2, doit.function3] foo = doit(func_list) 
0
source

You can use eval, which makes your program simpler and shorter.

 list= ["function1", "function2", "function3"] class doit(object): def __init__(self): for item in list: eval(item)() def function1(self): print "f1" def function2(self): print "f2" def function3(self): print "f3" 
-2
source

Source: https://habr.com/ru/post/921302/


All Articles