python – 如何动态调用类中的方法使用方法名分配给变量
发布时间:2021-01-11 14:56:35 所属栏目:Python 来源:互联网
导读:这个问题在这里已经有一个答案: Calling a function of a module from a string with the function’s name in Python10个 class MyClass: def __init__(self, i
|
这个问题在这里已经有一个答案:> Calling a function of a module from a string with the function’s name in Python10个 class MyClass:
def __init__(self,i):
self.i = i
def get(self):
func_name = 'function' + self.i
self.func_name() # <-- this does NOT work.
def function1(self):
//do something
def function2(self):
//do something
我得到的错误: 有人可以帮助这个.我已经尝试了很多排列和组合,但没有用! (注:’self.func_name’也不行) 解决方法def get(self):
def func_not_found(): # just in case we dont have the function
print "No Function "+self.i+" Found!"
func_name = 'function' + self.i
func = getattr(self,func_name,func_not_found)
func() # <-- this should work! (编辑:吉安站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- python – 在mod_wsgi下运行Django站点
- Django和Elastic Beanstalk URL运行状况检查
- python – 如何在数据帧中划分两列
- python – 根据列名拆分pandas数据框
- Python Popen shell = False导致OSError:[Errno 2]没有这样
- python – 关于Pandas Dataframe的Kurtosis doent工作
- 为什么我的Python模拟补丁以错误的顺序出现?
- Python,如何将状态/ update_with_media发布到Twitter?
- 如何在Python中有效地编码bigram计数和替换的字节对?
- 从python安装脚本中的编译标志中删除ppc
