python string to function: globals() vs sys.module vs dictionary

馋奶兔 提交于 2019-12-08 04:13:34

问题


I want to know what is the best way to map the string to a function. so far I know I can use:

  1. globals()[func_string]
  2. sys.modules[__name__]
  3. funcs_dictionary["func_string" : func]

Here is the code:

    >>> def foo(arg):
    ...     print "I'm foo: %s" % arg
    ... 
    >>> def bar(arg):
    ...     print "I'm bar: %s" % arg
    ... 
    >>> 
    >>> foo
    <function foo at 0xb742c7d4>
    >>> bar
    <function bar at 0xb742c80c>
    >>> 

globals()[func_string]:

    >>> def exec_funcs_globals(funcs_string):
    ...     for func_string in funcs_string:
    ...         func = globals()[func_string]
    ...         func("from globals() %s" % func)
    ... 
    >>> exec_funcs_globals(["foo", "bar"])
    I'm foo: from globals() <function foo at 0xb742c7d4>
    I'm bar: from globals() <function bar at 0xb742c80c>
    >>> 

sys.modules[__name__]:

    >>> import sys
    >>> 
    >>> def exec_funcs_thismodule(funcs_string):
    ...     thismodule = sys.modules[__name__]
    ...     for func_string in funcs_string:
    ...         func = getattr(thismodule, func_string)
    ...         func("from thismodule %s" % func)
    ... 
    >>> exec_funcs_thismodule(["foo", "bar"])
    I'm foo: from thismodule <function foo at 0xb742c7d4>
    I'm bar: from thismodule <function bar at 0xb742c80c>
    >>> 

funcs_dictionary["func_string" : func]:

    >>> funcs = {
    ...     "foo" : foo,
    ...     "bar" : bar
    ... }
    >>> 
    >>> def exec_funcs_dict(funcs_string):
    ...     for func_string in funcs_string:
    ...         func = funcs[func_string]
    ...         func("from thismodule %s" % func)
    ... 
    >>> exec_funcs_dict(["foo", "bar"])
    I'm foo: from thismodule <function foo at 0xb742c7d4>
    I'm bar: from thismodule <function bar at 0xb742c80c>

Originally I was worry about the sys.modules[__name__] will reload the modules and hurt the performance. But the code above seem to show that the function pointers are the same, so I guess I don't have to worry about it?

What is the best use case for the option 1, 2, 3?


回答1:


Using globals() is the normal way to access (and store) variables you want to access globally.

Otherwise, the usual choice is to implement a dispatch dictionary (like your option 3).

I've not seen the sys.modules approach used anywhere.




回答2:


I'm only a novice Python coder (< 6 months) but to answer your question directly, it actually seems to me that sys.modules is the "best" solution if you're looking for the most flexible. Here's why:

  1. Creating a new dictionary manually seems like wasted effort when the data already exists and can be accessed using both the globals() and sys.modules approaches.
  2. The globals() and sys.modules approaches also allow you to automatically and dynamically farm (and then filter) lists of functions - useful if, like me, you were trying to automatically build GUIs/menus from all functions defined in your code.
  3. A consideration with globals() is that if you import and call the "function farmer" from another module, you'll only get the functions of the "function farmer" module itself, not the parent module. [There might be a solution for this... I've posted a separate question here and will update this response if I hear anything useful back.]
  4. Using sys.modules on the other hand, you can call the "function farmer" and get the functions of the '__ main __' module.


来源:https://stackoverflow.com/questions/10220620/python-string-to-function-globals-vs-sys-module-vs-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!