python字符串函数:globals()vs sys.module vs dictionary [英] python string to function: globals() vs sys.module vs dictionary

查看:157
本文介绍了python字符串函数:globals()vs sys.module vs dictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道将字符串映射到函数的最佳方法是什么。
到目前为止我知道我可以使用:
$ b $ ol

  • globals()[func_string]

  • sys.modules [__ name __]

  • funcs_dictionary [func_string:func]

  • 是代码:

     >>> def foo(arg):
    ... print我是foo:%s%arg
    ...
    >>> def bar(arg):
    ... print我是酒吧:%s%arg
    ...
    >>>
    >>> foo
    <函数foo在0xb742c7d4>
    >>> bar
    <函数栏在0xb742c80c>
    >>>

    globals()[func_string]:

     >>> def exec_funcs_globals(funcs_string):
    ... for func_string in funcs_string:
    ... func =全局变量()[func_string]
    ... func(from globals()%s %func)
    ...
    >>> exec_funcs_globals([foo,bar])
    我是foo:来自globals()< function foo at 0xb742c7d4>
    我是bar:from globals()<函数栏在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])
    我是foo:从thismodule< function foo at 0xb742c7d4>
    我是bar:从thismodule<函数栏在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])
    我是foo:从thismodule< function foo at 0xb742c7d4>
    我是bar:从thismodule<功能栏在0xb742c80c>

    最初我担心sys.modules [__ name__]会重新加载模块并伤害性能。但上面的代码似乎表明函数指针是相同的,所以我想我不必担心它?



    什么是最佳用例选项1,2,3?

    解决方案

    使用全局变量()是常用的访问方式(和存储)你想要全局访问的变量。



    否则,通常的选择是实现一个调度字典(就像你的选项3)。
    $ b

    我没有看到任何地方使用的 sys.modules 方法。


    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?

    解决方案

    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.

    这篇关于python字符串函数:globals()vs sys.module vs dictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆