在Python中用同一个模块中的字符串调用函数?

时间:2022-12-24 23:14:44

Lets say I have a function bar inside a module called foo.py . Somewhere inside foo.py, I want to be able to call bar() from the string "bar". How do I do that?

假设我在一个名为foo.py的模块中有一个功能栏。在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

I know that there exists some built-in function in python called 'getattr'. However, it requires 'module object' to be the first parameter. How to obtain the 'module object' of the current module?

我知道python中存在一些名为'getattr'的内置函数。但是,它需要'模块对象'作为第一个参数。如何获取当前模块的“模块对象”?

3 个解决方案

#1


21  

globals is probably easier to understand. It returns the current module's __dict__, so you could do:

全局变量可能更容易理解。它返回当前模块的__dict__,因此你可以这样做:

func_I_want = globals()['bar']  #Get the function
func_I_want()    #call it

If you really want the module object, you can get it from sys.modules (but you usually don't need it):

如果你真的想要模块对象,你可以从sys.modules获取它(但你通常不需要它):

import sys.modules
this_mod = sys.modules[__name__]
func = getattr(this_mod,'bar')
func()

Note that in general, you should ask yourself why you want to do this. This will allow any function to be called via a string -- which is probably user input... This can have potentially bad side effects if you accidentally give users access to the wrong functions.

请注意,一般情况下,您应该问自己为什么要这样做。这将允许通过字符串调用任何函数 - 这可能是用户输入...如果您不小心让用户访问了错误的函数,这可能会产生潜在的不良副作用。

#2


11  

Use a dictionary that keeps the mapping of functions you want to call:

使用保存要调用的函数映射的字典:

if __name__ == '__main__':
  funcnames = {'bar': bar}
  funcnames['bar']()

#3


0  

How about this:

这个怎么样:

eval('bar' + '()')

#1


21  

globals is probably easier to understand. It returns the current module's __dict__, so you could do:

全局变量可能更容易理解。它返回当前模块的__dict__,因此你可以这样做:

func_I_want = globals()['bar']  #Get the function
func_I_want()    #call it

If you really want the module object, you can get it from sys.modules (but you usually don't need it):

如果你真的想要模块对象,你可以从sys.modules获取它(但你通常不需要它):

import sys.modules
this_mod = sys.modules[__name__]
func = getattr(this_mod,'bar')
func()

Note that in general, you should ask yourself why you want to do this. This will allow any function to be called via a string -- which is probably user input... This can have potentially bad side effects if you accidentally give users access to the wrong functions.

请注意,一般情况下,您应该问自己为什么要这样做。这将允许通过字符串调用任何函数 - 这可能是用户输入...如果您不小心让用户访问了错误的函数,这可能会产生潜在的不良副作用。

#2


11  

Use a dictionary that keeps the mapping of functions you want to call:

使用保存要调用的函数映射的字典:

if __name__ == '__main__':
  funcnames = {'bar': bar}
  funcnames['bar']()

#3


0  

How about this:

这个怎么样:

eval('bar' + '()')