如何检查具有给定名称的变量是否为非本地变量?

Meh*_*dad 2 python stack-frame python-3.x python-nonlocal

给定堆栈框架和变量名称,如何判断该变量是否为非局部变量?例:

import inspect

def is_nonlocal(frame, varname):
    # How do I implement this?
    return varname not in frame.f_locals  # This does NOT work

def f():
    x = 1
    def g():
        nonlocal x
        x += 1
        assert is_nonlocal(inspect.currentframe(), 'x')
    g()
    assert not is_nonlocal(inspect.currentframe(), 'x')

f()
Run Code Online (Sandbox Code Playgroud)

use*_*ica 5

检查框架的代码对象co_freevars,它是代码对象使用的闭包变量名称的元组:

def is_nonlocal(frame, varname):
    return varname in frame.f_code.co_freevars
Run Code Online (Sandbox Code Playgroud)

请注意,这是闭包变量,即nonlocal语句查找的变量类型.如果要包含非本地的所有变量,则应检查co_varnames(内部作用域中未使用的局部变量)和co_cellvars(内部作用域中使用的局部变量):

def isnt_local(frame, varname):
    return varname not in (frame.f_code.co_varnames + frame.f_code.co_cellvars)
Run Code Online (Sandbox Code Playgroud)

此外,不要混淆co_names,目前错误的文档.该inspect文件说co_names是局部变量,但是co_names是那种一个"一切"仓.它包括全局名称,属性名称和导入中涉及的几种名称 - 大多数情况下,如果预期执行实际上需要名称的字符串形式,那么它就会进入co_names.