python动态调用函数
1、类函数
-
- class TestA:
-
-
- def active_call_function(self,name):
- print("here is active_call_function.")
- # getaattr(module_name, function_name),module_name传self即可
- be_called_function = getattr(self, name)
- # 就直接调用。如果有其他参数,一样地传就好了
- be_called_function()
- pass
-
- def function_123(self):
- print("here is be_called_function.")
-
-
-
-
- if __name__ == '__main__':
-
- obj = TestA()
- obj.active_call_function("function_123")
带参数
-
- class TestA:
-
-
- def active_call_function(self,name,sss):
- print("here is active_call_function.")
- # getaattr(module_name, function_name),module_name传self即可
- be_called_function = getattr(self, name)
- # 就直接调用。如果有其他参数,一样地传就好了
- be_called_function(sss)
- pass
-
- def be_called_function_123(self,str11):
- print("here is be_called_function.","-----",str11)
-
-
-
-
- if __name__ == '__main__':
-
- obj = TestA()
- obj.active_call_function("be_called_function_123","test123")
2、普通函数
- def cl_pr(str2):
- print(str2)
-
-
-
- if __name__ == '__main__':
- eval("cl_pr")("test")
推荐阅读