范围规则的简短描述
Python 作用域规则到底是什么?
如果我有一些代码:
code1
class Foo:
code2
def spam.....
code3
for code4..:
code5
x()
在哪里找到x
?一些可能的选择包括下面的列表:
- 在封闭的源文件中
- 在类命名空间中
- 在函数定义
- 中 在 for 循环索引变量中
- 在 for 循环中
此外还有执行期间的上下文,当函数 spam
时被传递到其他地方。也许 lambda 函数 的传递方式有点不同?
某处必须有一个简单的参考或算法。对于中级 Python 程序员来说,这是一个令人困惑的世界。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
实际上,Python 作用域解析的简明规则,来自 学习 Python,第三篇。编辑。。 (这些规则特定于变量名称,而不是属性。如果您引用时不带句点,则这些规则适用。)
LEGB 规则
Local - 分配的名称函数内的任何方式(
def
或lambda
),并且未在该函数中声明为全局Enending-function — 在任何和所有静态封闭函数的本地范围内分配的名称 (
def
或lambda
),从内部到外部Global(模块) - 在模块文件顶层分配的名称,或通过在
中执行
global
语句分配的名称文件内的 defBuilt-in (Python) — 在内置名称模块中预先分配的名称:
open
、range
、SyntaxError
等因此,在
for
循环中没有自己的命名空间。按照 LEGB 顺序,范围将是
def spam
中的本地(在code3
、code4
和code5
中) )def
中)x
(在code1
中) >)?x
。x
永远不会在code2
中找到(即使在您可能期望的情况下,请参阅 Antti 的回答或此处)。Actually, a concise rule for Python Scope resolution, from Learning Python, 3rd. Ed.. (These rules are specific to variable names, not attributes. If you reference it without a period, these rules apply.)
LEGB Rule
Local — Names assigned in any way within a function (
def
orlambda
), and not declared global in that functionEnclosing-function — Names assigned in the local scope of any and all statically enclosing functions (
def
orlambda
), from inner to outerGlobal (module) — Names assigned at the top-level of a module file, or by executing a
global
statement in adef
within the fileBuilt-in (Python) — Names preassigned in the built-in names module:
open
,range
,SyntaxError
, etcSo, in the case of
The
for
loop does not have its own namespace. In LEGB order, the scopes would bedef spam
(incode3
,code4
, andcode5
)def
)x
declared globally in the module (incode1
)?x
in Python.x
will never be found incode2
(even in cases where you might expect it would, see Antti's answer or here).本质上,Python 中唯一引入新作用域的是函数定义。类是一种特殊情况,因为直接在主体中定义的任何内容都放置在类的命名空间中,但不能从它们包含的方法(或嵌套类)中直接访问它们。
在您的示例中,只有 3 个范围将在其中搜索 x:
spam 的范围 - 包含 code3 和 code5 中定义的所有内容(以及 code4,您的循环变量)
全局作用域 - 包含 code1 中定义的所有内容,以及 Foo(以及其后的任何更改)
内置命名空间。有一点特殊情况 - 它包含各种 Python 内置函数和类型,例如 len() 和 str()。一般来说,任何用户代码都不应该修改它,因此期望它包含标准函数而不包含其他内容。
仅当您在图片中引入嵌套函数(或 lambda)时才会出现更多作用域。
然而,它们的行为与您的预期非常相似。嵌套函数可以访问本地作用域中的所有内容,以及封闭函数作用域中的任何内容。例如。
限制:
可以访问本地函数变量以外的作用域中的变量,但如果没有进一步的语法,则无法重新绑定到新参数。相反,赋值将创建一个新的本地变量,而不是影响父作用域中的变量。例如:
为了在函数作用域内实际修改全局变量的绑定,您需要使用 global 关键字指定该变量是全局的。例如:
目前还没有办法对封闭函数作用域中的变量执行相同的操作,但Python 3引入了一个新的关键字“
nonlocal
”,它将以类似的方式起作用全局,但适用于嵌套函数范围。Essentially, the only thing in Python that introduces a new scope is a function definition. Classes are a bit of a special case in that anything defined directly in the body is placed in the class's namespace, but they are not directly accessible from within the methods (or nested classes) they contain.
In your example there are only 3 scopes where x will be searched in:
spam's scope - containing everything defined in code3 and code5 (as well as code4, your loop variable)
The global scope - containing everything defined in code1, as well as Foo (and whatever changes after it)
The builtins namespace. A bit of a special case - this contains the various Python builtin functions and types such as len() and str(). Generally this shouldn't be modified by any user code, so expect it to contain the standard functions and nothing else.
More scopes only appear when you introduce a nested function (or lambda) into the picture.
These will behave pretty much as you'd expect however. The nested function can access everything in the local scope, as well as anything in the enclosing function's scope. eg.
Restrictions:
Variables in scopes other than the local function's variables can be accessed, but can't be rebound to new parameters without further syntax. Instead, assignment will create a new local variable instead of affecting the variable in the parent scope. For example:
In order to actually modify the bindings of global variables from within a function scope, you need to specify that the variable is global with the global keyword. Eg:
Currently there is no way to do the same for variables in enclosing function scopes, but Python 3 introduces a new keyword, "
nonlocal
" which will act in a similar way to global, but for nested function scopes.关于Python3时间还没有彻底的答案,所以我在这里做了一个答案。此处描述的大部分内容在 4.2.2 解析中详细介绍Python 3 文档的名称。
正如其他答案中所提供的,有 4 个基本范围,即 LEGB,分别用于本地、封闭、全局和内置。除此之外,还有一个特殊的作用域,即类主体,它不包含类内定义的方法的封闭作用域;类主体中的任何赋值都会使变量从那里绑定到类主体中。
特别是,除了
def
和class
之外,no 块语句创建变量范围。在 Python 2 中,列表推导式不会创建变量作用域,但在 Python 3 中,列表推导式中的循环变量是在新的作用域中创建的。展示类体的特殊性
因此,与函数体不同的是,您可以在类体中将变量重新分配给同名的变量,从而获得同名的类变量;进一步查找此名称解析
改为类变量。
对于许多 Python 新手来说,最大的惊喜之一是
for
循环不会创建变量作用域。在Python 2中,列表推导式也不会创建作用域(而生成器和字典推导式会创建作用域!)相反,它们会泄漏函数或全局作用域中的值:推导式可以用作一种狡猾的(或者可怕的,如果你愿意的话)方式在 Python 2 中的 lambda 表达式中创建可修改的变量 - lambda 表达式确实会创建一个变量作用域,就像 def 语句一样,但在 lambda 中不允许使用任何语句。赋值作为 Python 中的语句意味着 lambda 中不允许进行变量赋值,但列表理解是一个表达式...
此行为已在 Python 3 中修复 - 没有理解表达式或生成器泄漏变量。
全局实际上意味着模块范围;主要的Python模块是
__main__
;所有导入的模块都可以通过 sys.modules 变量访问;要访问__main__
,可以使用sys.modules['__main__']
,或import __main__
;在那里访问和分配属性是完全可以接受的;它们将在主模块的全局范围中显示为变量。如果在当前作用域(类作用域除外)中曾经分配过名称,则它将被视为属于该作用域,否则它将被视为属于分配给该变量的任何封闭作用域(可能不会被分配)还没有,或者根本没有),或者最后是全局范围。如果变量被认为是本地变量,但尚未设置或已被删除,则读取变量值将导致
UnboundLocalError
,它是NameError
的子类。作用域可以使用 global 关键字声明它明确想要修改全局(模块作用域)变量:
即使它被隐藏在封闭作用域中,这也是可能的:
在 python 2 中,没有简单的方法来修改全局(模块作用域)变量中的值封闭范围;通常这是通过具有可变值来模拟的,例如长度为 1 的列表:
但是在 python 3 中,
nonlocal
来救援:nonlocal
文档 说即
nonlocal
始终指的是名称已绑定的最内外部非全局范围(即分配给,包括用作for
目标变量,在with
子句,或作为函数参数)。任何不被视为当前作用域或任何封闭作用域本地的变量都是全局变量。在模块全局字典中查找全局名称;如果没有找到,则从内置模块中查找全局;模块的名称从 python 2 更改为 python 3;在 python 2 中它是 __builtin__ ,在 python 3 中它现在称为
builtins
。如果您分配给内置模块的属性,则此后它将作为可读全局变量对任何模块可见,除非该模块使用其自己的同名全局变量隐藏它们。阅读内置模块也很有用;假设您希望在文件的某些部分使用 python 3 风格的打印函数,但文件的其他部分仍然使用
print
语句。在 Python 2.6-2.7 中,您可以通过以下方式获取 Python 3print
函数:from __future__ import print_function
实际上并不导入print
函数Python 2 中的任何位置 - 相反,它只是禁用当前模块中print
语句的解析规则,像任何其他变量标识符一样处理print
,从而允许print
函数是在内置函数中查找。There was no thorough answer concerning Python3 time, so I made an answer here. Most of what is described here is detailed in the 4.2.2 Resolution of names of the Python 3 documentation.
As provided in other answers, there are 4 basic scopes, the LEGB, for Local, Enclosing, Global and Builtin. In addition to those, there is a special scope, the class body, which does not comprise an enclosing scope for methods defined within the class; any assignments within the class body make the variable from there on be bound in the class body.
Especially, no block statement, besides
def
andclass
, create a variable scope. In Python 2 a list comprehension does not create a variable scope, however in Python 3 the loop variable within list comprehensions is created in a new scope.To demonstrate the peculiarities of the class body
Thus unlike in function body, you can reassign the variable to the same name in class body, to get a class variable with the same name; further lookups on this name resolve
to the class variable instead.
One of the greater surprises to many newcomers to Python is that a
for
loop does not create a variable scope. In Python 2 the list comprehensions do not create a scope either (while generators and dict comprehensions do!) Instead they leak the value in the function or the global scope:The comprehensions can be used as a cunning (or awful if you will) way to make modifiable variables within lambda expressions in Python 2 - a lambda expression does create a variable scope, like the
def
statement would, but within lambda no statements are allowed. Assignment being a statement in Python means that no variable assignments in lambda are allowed, but a list comprehension is an expression...This behaviour has been fixed in Python 3 - no comprehension expressions or generators leak variables.
The global really means the module scope; the main python module is the
__main__
; all imported modules are accessible through thesys.modules
variable; to get access to__main__
one can usesys.modules['__main__']
, orimport __main__
; it is perfectly acceptable to access and assign attributes there; they will show up as variables in the global scope of the main module.If a name is ever assigned to in the current scope (except in the class scope), it will be considered belonging to that scope, otherwise it will be considered to belonging to any enclosing scope that assigns to the variable (it might not be assigned yet, or not at all), or finally the global scope. If the variable is considered local, but it is not set yet, or has been deleted, reading the variable value will result in
UnboundLocalError
, which is a subclass ofNameError
.The scope can declare that it explicitly wants to modify the global (module scope) variable, with the global keyword:
This also is possible even if it was shadowed in enclosing scope:
In python 2 there is no easy way to modify the value in the enclosing scope; usually this is simulated by having a mutable value, such as a list with length of 1:
However in python 3, the
nonlocal
comes to rescue:The
nonlocal
documentation says thati.e.
nonlocal
always refers to the innermost outer non-global scope where the name has been bound (i.e. assigned to, including used as thefor
target variable, in thewith
clause, or as a function parameter).Any variable that is not deemed to be local to the current scope, or any enclosing scope, is a global variable. A global name is looked up in the module global dictionary; if not found, the global is then looked up from the builtins module; the name of the module was changed from python 2 to python 3; in python 2 it was
__builtin__
and in python 3 it is now calledbuiltins
. If you assign to an attribute of builtins module, it will be visible thereafter to any module as a readable global variable, unless that module shadows them with its own global variable with the same name.Reading the builtin module can also be useful; suppose that you want the python 3 style print function in some parts of file, but other parts of file still use the
print
statement. In Python 2.6-2.7 you can get hold of the Python 3print
function with:The
from __future__ import print_function
actually does not import theprint
function anywhere in Python 2 - instead it just disables the parsing rules forprint
statement in the current module, handlingprint
like any other variable identifier, and thus allowing theprint
the function be looked up in the builtins.范围稍微完整的示例:
输出:
A slightly more complete example of scope:
output:
Python 2.x 的范围规则已在其他答案中概述。我唯一要补充的是,在Python 3.0中,还有非本地作用域的概念(由“nonlocal”关键字表示)。这允许您直接访问外部作用域,并提供了执行一些巧妙技巧的能力,包括词法闭包(没有涉及可变对象的丑陋黑客)。
编辑:这是 PEP ,其中包含更多相关信息。
The scoping rules for Python 2.x have been outlined already in other answers. The only thing I would add is that in Python 3.0, there is also the concept of a non-local scope (indicated by the 'nonlocal' keyword). This allows you to access outer scopes directly, and opens up the ability to do some neat tricks, including lexical closures (without ugly hacks involving mutable objects).
EDIT: Here's the PEP with more information on this.
Python 通常使用三个可用的命名空间来解析变量。
有两个函数:
globals
和locals
,它们向您显示这两个命名空间的内容。命名空间由包、模块、类、对象构造和函数创建。没有任何其他风格的命名空间。
在这种情况下,对名为
x
的函数的调用必须在本地命名空间或全局命名空间中解析。在本例中,本地是方法函数
Foo.spam
的主体。全球性是——好吧——全球性。
规则是搜索方法函数(和嵌套函数定义)创建的嵌套局部空间,然后搜索全局。就是这样。
没有其他范围。
for
语句(以及其他复合语句,如if
和try
)不会创建新的嵌套作用域。仅定义(包、模块、函数、类和对象实例。)在类定义中,名称是类命名空间的一部分。例如,
code2
必须由类名限定。一般为Foo.code2
。然而,self.code2 也可以工作,因为 Python 对象将包含类视为后备。对象(类的实例)具有实例变量。这些名称位于对象的命名空间中。它们必须由对象限定。 (
variable.instance
。)在类方法中,您有局部变量和全局变量。您可以使用 self.variable 来选择实例作为命名空间。您会注意到,
self
是每个类成员函数的参数,使其成为本地命名空间的一部分。请参阅 Python 作用域规则、Python 范围,变量范围。
Python resolves your variables with -- generally -- three namespaces available.
There are two functions:
globals
andlocals
which show you the contents two of these namespaces.Namespaces are created by packages, modules, classes, object construction and functions. There aren't any other flavors of namespaces.
In this case, the call to a function named
x
has to be resolved in the local name space or the global namespace.Local in this case, is the body of the method function
Foo.spam
.Global is -- well -- global.
The rule is to search the nested local spaces created by method functions (and nested function definitions), then search global. That's it.
There are no other scopes. The
for
statement (and other compound statements likeif
andtry
) don't create new nested scopes. Only definitions (packages, modules, functions, classes and object instances.)Inside a class definition, the names are part of the class namespace.
code2
, for instance, must be qualified by the class name. GenerallyFoo.code2
. However,self.code2
will also work because Python objects look at the containing class as a fall-back.An object (an instance of a class) has instance variables. These names are in the object's namespace. They must be qualified by the object. (
variable.instance
.)From within a class method, you have locals and globals. You say
self.variable
to pick the instance as the namespace. You'll note thatself
is an argument to every class member function, making it part of the local namespace.See Python Scope Rules, Python Scope, Variable Scope.
未找到 x,因为您尚未定义它。 :-) 如果你把它放在那里,它可以在 code1(全局)或 code3(本地)中找到。
code2(类成员)对于同一类的方法内部的代码不可见——您通常可以使用 self.code2 来访问它们。 code4/code5(循环)与 code3 位于相同的范围内,因此如果您在其中写入 x,您将更改 code3 中定义的 x 实例,而不是创建新的 x。
Python 的作用域是静态的,因此如果将“spam”传递给另一个函数,spam 仍然可以访问它来自的模块中的全局变量(在 code1 中定义)以及任何其他包含作用域(见下文)。 code2 成员将再次通过 self 访问。
lambda 与 def 没有什么不同。如果在函数内部使用 lambda,则与定义嵌套函数相同。从 Python 2.2 开始,嵌套作用域可用。在这种情况下,您可以在函数嵌套的任何级别绑定 x,Python 将选取最里面的实例:
fun3 从最近的包含范围(即与 fun2 关联的函数范围)看到实例 x。但 fun1 中和全局定义的其他 x 实例不受影响。
在nested_scopes之前——在Python 2.1之前和2.1中,除非你特别要求使用from-future-import的功能——fun1和fun2的范围对fun3不可见,所以S.Lott的答案成立,你会得到全局x :
x is not found as you haven't defined it. :-) It could be found in code1 (global) or code3 (local) if you put it there.
code2 (class members) aren't visible to code inside methods of the same class — you would usually access them using self. code4/code5 (loops) live in the same scope as code3, so if you wrote to x in there you would be changing the x instance defined in code3, not making a new x.
Python is statically scoped, so if you pass ‘spam’ to another function spam will still have access to globals in the module it came from (defined in code1), and any other containing scopes (see below). code2 members would again be accessed through self.
lambda is no different to def. If you have a lambda used inside a function, it's the same as defining a nested function. In Python 2.2 onwards, nested scopes are available. In this case you can bind x at any level of function nesting and Python will pick up the innermost instance:
fun3 sees the instance x from the nearest containing scope, which is the function scope associated with fun2. But the other x instances, defined in fun1 and globally, are not affected.
Before nested_scopes — in Python pre-2.1, and in 2.1 unless you specifically ask for the feature using a from-future-import — fun1 and fun2's scopes are not visible to fun3, so S.Lott's answer holds and you would get the global x:
Python 名称解析 只知道以下几种scope:
print
、int
或zip
、def
块、lambda
表达式或推导式。def
块内,lambda
表达式或推导式,class
块内。值得注意的是,其他结构(例如
if
、for
或with
语句)没有自己的作用域。范围 TLDR:名称的查找从使用该名称的范围开始,然后是任何封闭范围(不包括类范围),再到模块全局变量,然后最后是内置函数 - 使用此搜索顺序中的第一个匹配项。
默认情况下,对范围的赋值是当前范围 - 必须使用特殊形式
nonlocal
和global
来赋值 为来自外部作用域的名称。最后,推导式和生成器表达式以及
:=
赋值表达式在组合时有一个特殊的规则。嵌套作用域和名称解析
这些不同的作用域构建了一个层次结构,内置函数然后全局始终形成基础,闭包、局部变量和类作用域按照词法定义进行嵌套。也就是说,只有源代码中的嵌套很重要,而不是调用堆栈等。
尽管
class
创建了一个作用域并且可能具有嵌套的类、函数和推导式,但是class
作用域的名称对于封闭的作用域是不可见的。这将创建以下层次结构:名称解析始终从访问名称的当前范围开始,然后沿层次结构向上移动,直到找到匹配项。例如,在
outer_function
和inner_function
中查找some_local
从各自的函数开始 - 并立即找到定义的some_local
分别在outer_function
和inner_function
中。当名称不是本地名称时,它会从定义它的最近的封闭范围中获取 - 在inner_function
搜索中查找some_closure
和print
直到 <分别是 code>outer_function 和内建函数。范围声明和名称绑定
默认情况下,名称属于它绑定到值的任何范围。在内部作用域中再次绑定相同的名称会创建一个具有相同名称的新变量 - 例如,
some_local
分别存在于outer_function
和inner_function
中。就作用域而言,绑定包括任何设置名称值的语句 - 赋值语句,还包括for
循环的迭代变量,或with
的名称代码>上下文管理器。值得注意的是,del
也算作名称绑定。当名称必须引用外部变量并且绑定在内部作用域中时,该名称必须声明为非本地名称。不同类型的封闭范围存在单独的声明:
nonlocal
始终引用最近的闭包,global
始终引用全局名称。值得注意的是,nonlocal
从不引用全局名称,而global
会忽略所有同名的闭包。没有引用内置范围的声明。值得注意的是,函数 local 和 nonlocal 是在编译时解析的。
非本地
名称必须存在于某个外部作用域中。相反,全局名称可以动态定义,并且可以随时在全局范围中添加或删除。推导式和赋值表达式
列表、集合和字典推导式以及生成器表达式的作用域规则几乎与函数相同。同样,赋值表达式的作用域规则几乎与常规名称绑定相同。
推导式和生成器表达式的范围与函数范围相同。作用域中绑定的所有名称(即迭代变量)都是推导式/生成器和嵌套作用域的局部变量或闭包。所有名称(包括可迭代对象)均使用函数内部适用的名称解析进行解析。
:=
赋值表达式适用于最近的函数、类或全局范围。值得注意的是,如果赋值表达式的目标已在最近的范围内声明为非本地或全局,则赋值表达式会像常规赋值一样遵循这一点。但是,推导式/生成器内的赋值表达式适用于推导式/生成器最近的封闭范围,而不是推导式/生成器本身的范围。当嵌套多个推导式/生成器时,将使用最近的函数或全局作用域。由于推导式/生成器作用域可以读取闭包和全局变量,因此赋值变量在推导式中也是可读的。从推导式分配给类范围是无效的。
虽然迭代变量对于其所绑定的推导式来说是局部的,但赋值表达式的目标不会创建局部变量,而是从外部作用域读取:
The Python name resolution only knows the following kinds of scope:
print
,int
, orzip
,def
block,lambda
expression or comprehension.def
block,lambda
expression or comprehension,class
block.Notably, other constructs such as
if
,for
, orwith
statements do not have their own scope.The scoping TLDR: The lookup of a name begins at the scope in which the name is used, then any enclosing scopes (excluding class scopes), to the module globals, and finally the builtins – the first match in this search order is used.
The assignment to a scope is by default to the current scope – the special forms
nonlocal
andglobal
must be used to assign to a name from an outer scope.Finally, comprehensions and generator expressions as well as
:=
asignment expressions have one special rule when combined.Nested Scopes and Name Resolution
These different scopes build a hierarchy, with builtins then global always forming the base, and closures, locals and class scope being nested as lexically defined. That is, only the nesting in the source code matters, not for example the call stack.
Even though
class
creates a scope and may have nested classes, functions and comprehensions, the names of theclass
scope are not visible to enclosed scopes. This creates the following hierarchy:Name resolution always starts at the current scope in which a name is accessed, then goes up the hierarchy until a match is found. For example, looking up
some_local
insideouter_function
andinner_function
starts at the respective function - and immediately finds thesome_local
defined inouter_function
andinner_function
, respectively. When a name is not local, it is fetched from the nearest enclosing scope that defines it – looking upsome_closure
andprint
insideinner_function
searches untilouter_function
and builtins, respectively.Scope Declarations and Name Binding
By default, a name belongs to any scope in which it is bound to a value. Binding the same name again in an inner scope creates a new variable with the same name - for example,
some_local
exists separately in bothouter_function
andinner_function
. As far as scoping is concerned, binding includes any statement that sets the value of a name – assignment statements, but also the iteration variable of afor
loop, or the name of awith
context manager. Notably,del
also counts as name binding.When a name must refer to an outer variable and be bound in an inner scope, the name must be declared as not local. Separate declarations exists for the different kinds of enclosing scopes:
nonlocal
always refers to the nearest closure, andglobal
always refers to a global name. Notably,nonlocal
never refers to a global name andglobal
ignores all closures of the same name. There is no declaration to refer to the builtin scope.Of note is that function local and
nonlocal
are resolved at compile time. Anonlocal
name must exist in some outer scope. In contrast, aglobal
name can be defined dynamically and may be added or removed from the global scope at any time.Comprehensions and Assignment Expressions
The scoping rules of list, set and dict comprehensions and generator expressions are almost the same as for functions. Likewise, the scoping rules for assignment expressions are almost the same as for regular name binding.
The scope of comprehensions and generator expressions is of the same kind as function scope. All names bound in the scope, namely the iteration variables, are locals or closures to the comprehensions/generator and nested scopes. All names, including iterables, are resolved using name resolution as applicable inside functions.
An
:=
assignment expression works on the nearest function, class or global scope. Notably, if the target of an assignment expression has been declarednonlocal
orglobal
in the nearest scope, the assignment expression honors this like a regular assignment.However, an assignment expression inside a comprehension/generator works on the nearest enclosing scope of the comprehension/generator, not the scope of the comprehension/generator itself. When several comprehensions/generators are nested, the nearest function or global scope is used. Since the comprehension/generator scope can read closures and global variables, the assignment variable is readable in the comprehension as well. Assigning from a comprehension to a class scope is not valid.
While the iteration variable is local to the comprehension in which it is bound, the target of the assignment expression does not create a local variable and is read from the outer scope:
在Python中,
如果在当前作用域中找不到变量,请参考LEGB命令。
In Python,
If a variable can't be found in the current scope, please refer to the LEGB order.