使用自由变量时是否创建命名空间?
Is the namespace created when free variables are used?
x = 2
def my_func():
print(x)
print(dir())
my_func()
语句 print(dir())
的输出是一个空列表 []
。本地命名空间是否已创建但未包含任何名称或根本未创建?
x
是全局的一部分,您可以通过
进行检查
x = 2
def my_func():
print(x)
print(locals())
print(globals())
my_func()
print(locals())
输出
{}
和print(globals())
输出
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x004FC230>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:/Users/leeron.arad/Documents/test-proj/venv/t.py', '__cached__': None, 'x': 2, 'my_func': <function my_func at 0x0058E978>}
在全局字典中,您可以看到在 my_func
范围内打印的 'x': 2
。
x
值不是在函数中创建的,它仅来自全局范围。
如果您在 dir()
中传递任何参数,例如:
x = 2
def my_func():
print(x)
print(dir(1))
my_func()
运行 这个文件,你可能会得到这样的答案:
2
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
x = 2
def my_func():
print(x)
print(dir())
my_func()
语句 print(dir())
的输出是一个空列表 []
。本地命名空间是否已创建但未包含任何名称或根本未创建?
x
是全局的一部分,您可以通过
x = 2
def my_func():
print(x)
print(locals())
print(globals())
my_func()
print(locals())
输出
{}
和print(globals())
输出
{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x004FC230>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'C:/Users/leeron.arad/Documents/test-proj/venv/t.py', '__cached__': None, 'x': 2, 'my_func': <function my_func at 0x0058E978>}
在全局字典中,您可以看到在 my_func
范围内打印的 'x': 2
。
x
值不是在函数中创建的,它仅来自全局范围。
如果您在 dir()
中传递任何参数,例如:
x = 2
def my_func():
print(x)
print(dir(1))
my_func()
运行 这个文件,你可能会得到这样的答案:
2
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']