【发布时间】:2012-10-02 12:01:23
【问题描述】:
假设我在一个名为 foo.py 的模块中有一个函数 bar 。在 foo.py 的某个地方,我希望能够从字符串“bar”调用 bar()。我该怎么做?
# filename: foo.py
import sys
def bar():
print 'Hello, called bar()!'
if __name__ == '__main__':
funcname = 'bar'
# Here I should be able to call bar() from funcname
我知道在 python 中存在一些名为“getattr”的内置函数。但是,它需要“模块对象”作为第一个参数。如何获取当前模块的“模块对象”?
【问题讨论】: