前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python数据分析(中英对照)·Scope Rules范围规则

Python数据分析(中英对照)·Scope Rules范围规则

作者头像
数媒派
发布2022-12-01 15:23:33
2740
发布2022-12-01 15:23:33
举报
文章被收录于专栏:产品优化

2.1.1: Scope Rules范围规则

Let’s talk about scope rules next. 接下来我们来讨论范围规则。 Consider a situation where, in different places of your code,you have to find several functions called "update," 考虑一个情况,在代码的不同地方,你必须找到几个叫做“更新”的函数。 or several variables called "x." 或者称为“x”的几个变量 How do we know which update function or which x variable Python uses at any given time? 我们如何知道Python在任何给定时间使用哪个更新函数或哪个x变量? We know from before that each variable name belongs to a certain abstract environment or namespace,and we can think of it as a context in which a given name exists. 我们从前面就知道,每个变量名都属于某个抽象环境或名称空间,我们可以将其视为给定名称所在的上下文。 This is analogous to many everyday situations that require a context for names to be meaningful. 这类似于许多日常情况,这些情况要求名称具有意义的上下文。 So when you talk about your friend John, your other friends know intuitively which John you’re talking about. 所以当你谈论你的朋友约翰时,你的其他朋友直觉地知道你在谈论哪个约翰。 But how does Python know which update function to call or which variable x to use? 但是Python如何知道调用哪个更新函数或使用哪个变量x呢? The answer is that it uses so-called "scope rules" to make this determination. 答案是,它使用所谓的“范围规则”来做出这一决定。 It searches for the object, layer by layer,moving from inner layers towards outer layers,and it uses the first update function or the first x variable that it finds. 它逐层搜索对象,从内层移动到外层,并使用它找到的第一个更新函数或第一个x变量。 You can memorize this scope rule by the acronym LEGB. 你可以用首字母缩略词LEGB来记住这个范围规则。 It may not be the catchiest acronym, but it works. 它可能不是最吸引人的首字母缩略词,但它很管用。 Here’s what the different letters stand for. 以下是不同字母的含义。 L stands for "local," E stands for "enclosing function," G for "global,"and B stands for "built-in." L代表“本地”,E代表“封闭函数”,G代表“全局”,B代表“内置” In other words, local is the current function you’re in. 换句话说,local是您当前使用的函数。 Enclosing function is the function that called the current function, if any. 封闭函数是调用当前函数(如果有)的函数。 Global refers to the module in which the function was defined. 全局是指定义函数的模块。 And built-in refers to Python’s built-in namespace. 内置是指Python的内置名称空间。 If Python cannot resolve the name, meaning if it cannot find an object having the name you’ve requested, it raises a so-called "name error exception." 如果Python无法解析名称,这意味着如果它找不到具有您请求的名称的对象,则会引发所谓的“名称错误异常” And the execution of your program typically stops at this point. 程序的执行通常在这一点停止。 Let’s practice scope rules first by defining a function called "update" that has no parameters– def update parentheses and colon. 让我们首先通过定义一个名为“update”的函数来实践范围规则,该函数没有参数——def update圆括号和冒号。 Then we have x dot append 1. 然后我们有x点加1。 All this function does is that it appends, or at least tries to append,the number 1 to an object called x. 此函数所做的只是将数字1附加到或至少尝试附加到名为x的对象。 When we run this function update, Python gives us a name error and tells us that a name x is not defined. 当我们运行这个函数更新时,Python给了我们一个名称错误,并告诉我们没有定义名称x。 Let’s see what happens if we define a list called "x," and then call the function. 让我们看看如果我们定义一个名为“x”的列表,然后调用函数会发生什么。 x is a list consisting of two elements, 1 and 1. x是一个由两个元素组成的列表,1和1。 And then we call the function "update." 然后我们调用函数“update” You might be surprised to see that the code now runs and appears to modify the list x. 您可能会惊讶地看到,代码现在正在运行并似乎在修改列表x。 To understand what is happening, let’s go through the steps that Python goes through when it tries to find object x. 为了理解正在发生的事情,让我们看一下Python在尝试查找对象x时所经历的步骤。 Using the LEGB rule, it first looks for x in the local scope,inside the function update. 使用LEGB规则,它首先在函数update内部的本地范围中查找x。 But x does not exist within this scope. 但是x不在这个范围内。 It then moves on to the next layer, which is enclosing functions. 然后,它移动到下一层,即封闭函数。 But in this case, there are no enclosing functions,because we didn’t call the update function from inside another function. 但是在本例中,没有封闭函数,因为我们没有从另一个函数内部调用update函数。 So let’s move on to the next layer, which is the global layer, the module or namespace where the function update was defined. 现在让我们转到下一层,即全局层,即定义函数更新的模块或名称空间。 The global layer or scope does contain an object called x. 全局层或作用域包含一个名为x的对象。 It is the x that we defined right here. 这就是我们在这里定义的x。 And since it is the first object of that name that Python found,this is the object that it will use. 因为它是Python找到的第一个同名对象,所以它将使用这个对象。 This example focusing in scope rules shows that you can manipulate global variables,variables defined in the global scope, from within functions. 这个关注范围规则的示例显示,您可以从函数内部操作全局变量,即在全局范围中定义的变量。 Functions that modify either their inputs or objects defined in other parts of the program in this type of behind the scenes manner are said to have "side effects." 以这种幕后方式修改其输入或程序其他部分中定义的对象的函数称为“副作用” For example, if you look at the function call, which in this case is simply update followed by parentheses,it is not clear that the function has side effects. 例如,如果您查看函数调用,在本例中,函数调用只是update,后跟括号,则不清楚函数是否有副作用。 This is a programming style that you should generally avoid,because it can lead to programming errors that are very difficult to find. 这是一种通常应该避免的编程风格,因为它会导致很难发现的编程错误。 Let’s then consider a slightly more complex example about scope rules. 让我们考虑一个关于范围规则的更复杂的例子。 This example demonstrates both scope rules and mutability and immutability of Python objects. 这个示例演示了Python对象的作用域规则以及可变性和不变性。 To get going, let’s first write two short functions. 首先,我们编写两个简短的函数。 Our first function is called "update." 我们的第一个函数叫做“更新” And our second function is called "main." 我们的第二个函数叫做“main” In this case, the main function calls the update function as part of the program. 在这种情况下,main函数调用update函数作为程序的一部分。 We would like to know what are the values of n and x at different points of the program’s execution. 我们想知道在程序执行的不同点上n和x的值是多少。 Let’s try to figure it out first without running the code,and we can then check our answer against a program’s output. 让我们先尝试在不运行代码的情况下找出答案,然后根据程序的输出检查答案。 Let’s first get a sense of the program’s overall flow. 让我们首先了解一下程序的总体流程。 We first call main. 我们先打电话给梅因。 Main calls update. 主呼叫更新。 The execution then returns to main. 然后执行返回到main。 And once main exits, we return the global scope,the scope where we defined the functions main and update. 一旦main退出,我们将返回全局范围,即定义main和update函数的范围。 Let’s first look at the code in main. 让我们首先看看main中的代码。 Here, n and x are assigned in the function main,so when we get to the first print function,Python finds the objects n and x in the local scope,meaning that the values of the objects are n equal to 1 and x is equal to 0, 1, 2, 3, a list. 在这里,n和x在函数main中赋值,因此当我们进入第一个print函数时,Python会在局部范围内找到对象n和x,这意味着对象的值n等于1,x等于0,1,2,3,一个列表。 The program then calls the function update with arguments and n and x. 然后,程序使用参数n和x调用函数update。 So the program’s control then moves to the function update. 因此,程序的控制随后移动到函数更新。 All of the function update has a parameter, n. 所有函数更新都有一个参数n。 On the first line of the function, we have an assignment,n equals 2, which creates a new variable that happens to be called "n,"And its value is set to 2. 在函数的第一行,我们有一个赋值,n等于2,它创建了一个新的变量,恰好被称为“n”,它的值被设置为2。 On the second line, we append the number 4 to the object x, which is one of the function’s parameters. 在第二行,我们将数字4附加到对象x,这是函数的参数之一。 Just to be clear on terminology, remember that an argument is an object that is passed to a function as its input when the function is called. 为了明确术语,请记住参数是在调用函数时传递给函数作为其输入的对象。 A parameter, in contrast, is a variable that is used in the function definition to refer to that argument. 相反,参数是函数定义中用于引用该参数的变量。 That means that the second line will append the number 4 to the list that we have created in the main function. 这意味着第二行将把数字4附加到我们在main函数中创建的列表中。 So when we get to the print function, Python finds the objects n and x in the local scope, 当我们使用print函数时,Python会在局部范围内找到对象n和x, meaning that the values of the objects are n equal to 2 and x is a list, 0, 1, 2, 3, 4. 这意味着对象的值n等于2,x是一个列表,0、1、2、3、4。 Python then exits the function and control returns to the main function. Python然后退出该函数,控件返回主函数。 Remember that variables that are created within a function, such as n in the case of the update function, are deleted as soon as Python exits that function. 请记住,在函数中创建的变量(如update函数中的n)将在Python退出该函数后立即删除。 Looking at the final print function, the objects n and x are found in the local scope where n is equal to 1 and x is a list, 0, 1, 2, 3 4. 查看最终的打印函数,对象n和x位于局部范围内,其中n等于1,x是一个列表,0、1、2、3和4。 We can check our reasoning by running the program. 我们可以通过运行程序来检查我们的推理。 If this example or some of the steps that we have just gone through are unclear to you, go through the example again, at each point using the LEGB rule to figure out how Python determines which n and x object to use. 如果您不清楚这个示例或我们刚刚完成的一些步骤,请再次查看该示例,在每一点上使用LEGB规则来确定Python如何确定要使用哪个n和x对象。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 2.1.1: Scope Rules范围规则
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档