AndyAO
V2EX  ›  问与答

如何为 Python 的函数参数设置调用者上下文的表达式默认值?

  •  2
     
  •   AndyAO · Mar 23, 2021 · 1442 views
    This topic created in 1895 days ago, the information mentioned may be changed or developed.

    想在 Python 3 上封装个将文件作为代码动态执行的函数,exec的参数globals,locals 的默认值分别是当前上下文globals()locals(),自己封装的函数如何实现这种效果?

    1. 已经尝试过 def file(filepath,globals=globals(),locals=locals()) 没有效果。
    2. 查看exec的实现,发现被定义在def exec(__object: Union[str, bytes, CodeType], __globals: Optional[Dict[str, Any]] = ..., __locals: Optional[Mapping[str, Any]] = ...) -> Any: ...,直接使用这个参数列表会导致语法错误。

    目前的函数是这样定义的

    def file(filepath,globals,locals):
        with open(filepath, 'r', encoding='utf8') as textFileObj:
            return exec(compile(textFileObj.read(), filepath, 'exec'),globals,locals)
    

    调用的时候只能这样手动传入,虽然可以通过片段等方法简化这个过程,实现类似的效果,但总归是默认值更方便。

    Exe.file(Path.here(__file__,'HelloWorld.py'),globals(),locals())
    
    Supplement 1  ·  Mar 23, 2021
    我的问题已经得到了解决,目前看来,Python 没有这样的机制,另外如果要使用表达式来定义默认参数,那么很可能会出现令人意想不到的现象,从这里可以察觉出 Python 函数的某种本质,下面这个高票答案很好的讲述了这个问题,很值得去看。

    [“Least Astonishment” and the Mutable Default Argument]( https://stackoverflow.com/a/1145781)
    2 replies    2021-03-23 14:26:33 +08:00
    ebingtel
        1
    ebingtel  
       Mar 23, 2021
    这样?

    a.py:

    import inspect


    def f():
    print(inspect.currentframe().f_back.f_globals)


    调用:

    >>> b = 1
    >>> m = __import__('a')
    >>> m.f()
    {'b': 1, '__builtins__': <module '__builtin__' (built-in)>, 'm': <module 'a' from 'a.py'>, '__package__': None, '__name__': '__main__', '__doc__': None}
    wangaroo
        2
    wangaroo  
       Mar 23, 2021
    global 关键字?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   855 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 20:12 · PVG 04:12 · LAX 13:12 · JFK 16:12
    ♥ Do have faith in what you're doing.