如何按名称动态调用对象上的函数?

2024-04-24 05:53:17 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python中,假设我有一个字符串,其中包含我知道某个特定对象将具有的类函数的名称,我如何调用它?

即:

obj = MyClass() # this class has a method doStuff()
func = "doStuff"
# how to call obj.doStuff() using the func variable?

Tags: to对象函数字符串名称objmyclasscall