全局变量和局部变量命名规则

PYTHON开发人员的提示 (TIPS FOR PYTHON DEVELOPERS)

In the beginning, I assume that you know how to define your own functions — but not only that, you know how to write functions with multiple parameters and can return multiple values using tuples.

首先,我假设您知道如何定义自己的函数-不仅如此,您还知道如何编写具有多个参数的函数并可以使用元组返回多个值。

先决条件 (Prerequisites)

If you do not familiar with defining your own function, the article below will give you more information about it.

如果您不熟悉定义自己的函数,则下面的文章将为您提供有关它的更多信息。

We will now discuss the idea of scope in the context of user-defined functions. You have been defining variables in your programs. So far, you have been using these variables without any problems. However, it would be best if you remembered that not all objects you define are always accessible everywhere in a program. This is the idea of scope, which tells you which part of a program an object or a variable may be accessed.

现在,我们将在用户定义函数的上下文中讨论范围的概念。 您一直在程序中定义变量。 到目前为止,您一直在使用这些变量,没有任何问题。 但是,最好记住,并非定义的所有对象始终在程序中的任何地方都可以访问。 这是范围的概念,它告诉您可以访问程序的哪个部分的对象或变量。

The variables or objects, such as functions that are defined in your program, have a name, as does the function.

变量或对象(例如程序中定义的函数)的名称与函数相同。

There are several types of scope. The first one is the global scope, which means that it is defined in the main body of a script. The second one is the local scope. Local scope means that it is defined within a function. Once the function’s execution is done, any variable inside the local scope terminates, which means you cannot access those variables anymore outside of the function definition.

范围有几种类型。 第一个是全局作用域 ,这意味着它是在脚本主体中定义的。 第二个是本地范围 。 局部作用域意味着它是在函数中定义的。 一旦执行完函数,本地作用域内的任何变量都将终止,这意味着您无法再在函数定义之外访问这些变量。

The third is the built-in scope. This consists of variables in the pre-defined built-ins module, which provides by Python, such as print and sum. The last one is enclosing functions, and we will discuss this later in the nested function section.

第三是内置示波器 。 它由预定义的内置模块中的变量组成,该模块由Python提供,例如print和sum。 最后一个是封闭函数 ,稍后我们将在嵌套函数部分中对此进行讨论。

Let’s check out an example here.

让我们在这里查看示例。

Author作者

We define the function and then call it. If we try to access the variable name value before or after function execution, the variable is not accessible. This is because it was defined only within the local scope of the function. The variable value was not defined globally.

我们定义函数,然后调用它。 如果我们尝试在函数执行之前或之后访问变量名称value ,则无法访问该变量。 这是因为它仅在函数的本地范围内定义 。 变量value未全局定义。

What if we define the variable globally before defining and calling the function?

如果在定义和调用函数之前全局定义变量怎么办?

Author作者

In short, any time we call the variable in the global scope, it will access the variable name in the global scope. However, any time we call the variable in the local scope of the function, it will look in the local scope first. That’s why calling square(5) returns results in 25 and not 30.

简而言之,每当我们在全局范围内调用变量时,它将访问全局范围内的变量名称。 但是,每当我们在函数的本地范围内调用变量时,它将首先在本地范围内查找。 这就是为什么调用square(5)返回结果为25而不是30的原因。

If Python cannot find the variable in the local scope, it will then look in the global scope. For example, we access a variablevalue defined globally within the function square. Note that the global value accessed is the value at the time the function is called, not the value when the function is defined. Thus, if we re-assign value and call the function, we see that the new value of value is accessed.

如果Python在本地范围内找不到变量,则它将在全局范围内查找。 例如,我们访问在函数正方形内全局定义的变量value 。 请注意,访问的全局值是调用函数时的值,而不是定义函数时的值。 因此,如果我们重新分配value并调用该函数,则会看到访问了value的新值。

It is clear that when we reference a variable, the local scope is first searched, then the global. The built-in scope is reached if the variable does not exist in the local and global scope. What if we want to alter the value of a global variable within a function call? This is where the keyword global comes in handy.

显然,当我们引用变量时,首先搜索局部范围,然后是全局范围。 如果变量在本地和全局范围中不存在,则将达到内置范围。 如果我们想在函数调用中更改全局变量的值怎么办? 这是关键字global派上用场的地方。

Author作者

Within the function definition, we use the keyword global, followed by the variable name of the global variable that we wish to access and alter. For example, here we change value to its square. After that, we will call the value variable. We see that the global value has indeed been squared by running the function square.

在函数定义中,我们使用关键字global ,后跟我们要访问和更改的全局变量的变量名。 例如,在这里我们将value更改为其平方。 之后,我们将调用value变量。 我们看到,通过运行函数平方,确实实现了全局值的平方。

嵌套函数 (Nested Function)

What if we have a function called inside which is defined inside function outside, and we reference a variable name x in the inside function? The answer is intuitive. Python searches the local scope of the inside function. If it doesn’t find that variable, it searches the scope of the outside function, which is called an enclosing function because it encloses the inside function. If Python can’t find that variable in the enclosing function’s scope, it only then searches the global scope and then the built-in scope.

如果我们有一个名为inside的函数inside该函数outside的函数inside定义,并且在inside函数中引用了变量名x ,该怎么办? 答案很直观。 Python搜索inside函数的本地范围。 如果找不到该变量,它将搜索outside函数的范围,该函数称为封闭函数,因为它将inside函数封闭起来。 如果Python在封闭函数的作用域中找不到该变量,则只会先搜索全局作用域,然后再搜索内置作用域。

Author作者

Why are we nesting a function?

为什么我们要嵌套一个函数?

There are some good reasons. Let’s say that we want to do a process multiple times within a function. For example, we want a function that accepts three numbers as parameters and executes the same function on each of them. One way would be to write out the computation three times, but this does not scale if you want to perform it often. Instead, we can define an inner function within our function definition and call it where required. This is called a nested function.

有一些很好的理由。 假设我们要在一个函数中进行多次处理。 例如,我们想要一个函数,该函数接受三个数字作为参数,并对每个数字执行相同的函数。 一种方法是将计算写出3次,但是如果您想经常执行它就无法扩展。 相反,我们可以在函数定义内定义内部函数,并在需要时调用它。 这称为嵌套函数。

Let’s look at another example here.

让我们在这里看看另一个例子。

Author作者

The syntax for the inside function is the same as that for any other function. In this example, we define a function power_of, which contains an internal function called inside. Now look at what power_of returns: it returns the internal function inside. power_of takes one argument and creates a function inside that returns the nth power of any number. This is a little bit complicated and will be more precise when we execute the function power_of.

inside函数的语法与任何其他函数的语法相同。 在此示例中,我们定义一个函数power_of ,其中包含一个称为inside的内部函数。 现在看看power_of返回什么:它返回内部函数insidepower_of一个参数并inside创建一个函数,该函数返回任意数量的n次幂。 这有点复杂,当我们执行power_of函数时,它将更加精确。

Passing the number 2 to power_of creates a function that squares any number. Likewise, passing the number 3 to power_of creates a function that cubes any number.

将数字2传递给power_of会创建一个对任何数字平方的函数。 同样,将数字3传递给power_of会创建一个对任何数字power_of立方的函数。

One interesting detail is, when we call the function square, it remembers the value n=2, although the enclosing scope defined by power_of and to which n=2 is local, has finished execution. This is a nuance referred to as a closure in Computer Science circles and shouldn’t concern you too much. However, it is worth mentioning, as you may encounter it out there.

一个有趣的细节是,当我们调用函数square ,它记住值n=2 ,尽管power_of定义的且n=2是局部的包围范围已经完成了执行。 这是计算机科学界中被称为闭包的细微差别,不要太在意您。 但是,值得一提的是,您可能会在那里遇到它。

Author作者

Turning into our discussion of scope, you can use the keyword global in function definitions to create and change global variables; similarly, in a nested function, you can use the keyword nonlocal to create and change variables in an enclosing scope.

进入我们关于范围的讨论,您可以在函数定义中使用关键字global来创建和更改全局变量。 同样,在嵌套函数中,可以使用关键字nonlocal在封闭范围内创建和更改变量。

In this example, we modify the value of n in the inside function. Because we used the keyword nonlocal, it changes the value of n in the enclosing scope. This is why calling the outside function prints the value of n as determined within the function inside.

在此示例中,我们在inside函数中修改了n的值。 因为我们使用了关键字nonlocal ,所以它在封闭范围内更改了n的值。 这就是为什么调用outside函数会打印在函数inside确定的n值的原因。

结论 (Conclusion)

Variable references search:

变量引用搜索:

  • Local scope当地范围
  • Enclosing functions封闭功能
  • Global scope全球范围
  • Built-in scope内置范围

This is recognized as the LEGB rule, where L is for local, E is for enclosing, G is for global, and B is for built-in. Also, remember that defining variables will only create or change local names, unless they are stated in global or nonlocal statements using the keyword global or the keyword nonlocal.

这被认为是LEGB规则,其中L表示本地,E表示封闭,G表示全局,B表示内置。 另外,请记住,除非在全局或非本地语句中使用关键字global或关键字nonlocal声明变量,否则定义变量只会创建或更改本地名称。

Other Interesting Articles#1 Function Arguments: Default, Keyword, and Arbitrary#2 Writing Your Own Functions#3 Python: Procedural or Object-Oriented Programming?#4 Data Science with Python: How to Use NumPy Library#5 Do you have the Software Engineer and Data Scientist skills?

关于作者 (About the Author)

Wie Kiang is a researcher who is responsible for collecting, organizing, and analyzing opinions and data to solve problems, explore issues, and predict trends.

Wie Kiang是一位研究人员,负责收集,组织和分析意见和数据以解决问题,探索问题和预测趋势。

He is working in almost every sector of Machine Learning and Deep Learning. He is carrying out experiments and investigations in a range of areas, including Convolutional Neural Networks, Natural Language Processing, and Recurrent Neural Networks.

他几乎在机器学习和深度学习的每个领域工作。 他正在许多领域进行实验和研究,包括卷积神经网络,自然语言处理和递归神经网络。

Connect on LinkedIn

LinkedIn上 连接

翻译自: https://towardsdatascience.com/scope-of-variable-and-legb-rule-4d44d4576df5

全局变量和局部变量命名规则


http://www.taodudu.cc/news/show-863493.html

相关文章:

  • dask 使用_在Google Cloud上使用Dask进行可扩展的机器学习
  • 计算机视觉课_计算机视觉教程—第4课
  • 用camelot读取表格_如何使用Camelot从PDF提取表格
  • c盘扩展卷功能只能向右扩展_信用风险管理:功能扩展和选择
  • 使用OpenCV,Keras和Tensorflow构建Covid19掩模检测器
  • 使用Python和OpenCV创建自己的“ CamScanner”
  • cnn图像进行预测_CNN方法:使用聚合物图像预测其玻璃化转变温度
  • 透过性别看世界_透过树林看森林
  • gan神经网络_神经联觉:当艺术遇见GAN
  • rasa聊天机器人_Rasa-X是持续改进聊天机器人的独特方法
  • python进阶指南_Python特性工程动手指南
  • 人工智能对金融世界的改变_人工智能革命正在改变网络世界
  • 数据科学自动化_数据科学会自动化吗?
  • 数据结构栈和队列_使您的列表更上一层楼:链接列表和队列数据结构
  • 轨迹预测演变(第1/2部分)
  • 人口预测和阻尼-增长模型_使用分类模型预测利率-第3部分
  • 机器学习 深度学习 ai_人工智能,机器学习,深度学习-特征和差异
  • 随机模拟_随机模拟可帮助您掌握统计概念
  • 机器学习算法如何应用于控制_将机器学习算法应用于NBA MVP数据
  • 知乎 开源机器学习_使用开源数据和机器学习预测海洋温度
  • :)xception_Xception:认识Xtreme盗梦空间
  • 评估模型如何建立_建立和评估分类ML模型
  • 介绍神经网络_神经网络介绍
  • 人物肖像速写_深度视频肖像
  • 奇异值值分解。svd_推荐系统-奇异值分解(SVD)和截断SVD
  • 机器学习 对模型进行惩罚_使用Streamlit对机器学习模型进行原型制作
  • 神经网络实现xor_在神经网络中实现逻辑门和XOR解决方案
  • sagan 自注意力_请使用英语:自我注意生成对抗网络(SAGAN)
  • pytorch 音频分类_Pytorch中音频的神经风格转换
  • 变压器 5g_T5:文本到文本传输变压器

全局变量和局部变量命名规则_变量范围和LEGB规则相关推荐

  1. mysql数据库修改列排序规则_更改数据库排序规则

    在SQLServer2008R2版本及以上,我们选择的都是默认安装,所以在创建数据库的时候,服务器默认选择的排序规则是一个SQL_Latin1_General_CP1_CI_AS排序规则,这样在向数据 ...

  2. 全局变量和局部变量的区别_值得收藏!8大技巧,带你了解菜鸟和高手的区别!...

    对于Python编程者而言,或许大家都认为Python是非常简单易学的,在学习一段时间的Python之后,都认为自己的Python编程水平已经非常好了,但是python编程绝对不是简单的几句语法就可以 ...

  3. js全局变量和局部变量名称一样_微信小程序的全局变量、页面变量,你真的掌握了?...

    开发微信小程序时,遇到的坑挺多的,别的不说,单是变量的应用,就够你折腾一阵子的了,可能,或许是我不熟悉的缘故吧? 1 如果你认为你很熟悉的话,那你猜一下,下面的变量-userInfo,是属于全局变量, ...

  4. 在python中、关于全局变量和局部变量、以下_关于全局变量和局部变量-Python

    练习: No.1 num = 100 def func(): num = 123 print(num) func() 先 想 一 下 : 输出: 123 解析: 函数内部的变量名,如果第一次出现,且出 ...

  5. mysql全局变量和局部变量的区别_详细讲解mysql全局变量与局部变量

    通常在服务器启动时,会将每个全局变量初始化为其默认值(我们可以通过命令行或选项文件中指定的选项更改这些默认值),然后服务器还为每个连接的客户端维护一组会话变量,客户端的会话变量在连接时使用相应全局变量 ...

  6. 电子邮件地址取名规则_最佳电子邮件设计规则:内容

    电子邮件地址取名规则 Photo: Svenstorm 照片:Svenstorm This installment of Rules for Best Practice in Email (Part ...

  7. 楷书书法规则_记住这些“潜规则”,小楷不等于把楷书简单写小

    常说:习字须先习大字,而后再学小楷,这话也不无道理,由于小楷字形较小,要表达用笔和结构的美感,确实比较难.不会写小楷的人,总以为写字的动作只要比写大楷的动作小一些就行,可事实上并非如此.小楷字形较小, ...

  8. java变量命名规则_浅谈JAVA开发规范与开发细节(上)

    开发团队在开发过程中,由于每个人的开发习惯,以及对于技术的理解深浅程度不一,往往一个项目在开发过程中,代码的质量,代码的风格都不尽相似,所以有一份适合团队的代码规范是非常有必要的,而一个团队的代码规范 ...

  9. Python入门--局部变量,全局变量,作用域,LEGB规则

    #变量的作用域-->程序代码能访问该变量的区域.变量可以被访问的范围.变量发挥作用的范围. #根据变量的有效范围,可分为 #局部变量 #1,在函数内定义并使用的变量,只在函数内部有效, # 局部 ...

最新文章

  1. 简单几行代码,写一个百度广告屏蔽插件,爽到爆
  2. [算法题] Search in Rotated Sorted Array ii
  3. oracle数据库的医院信息系统数据库升级方案,医院信息系统数据库从Oracle8i到10gR2升级的实现...
  4. 047 一维数据的格式化和处理
  5. 今天中午过的不爽的原因分析
  6. AVFoundation 之数字媒体(音频)
  7. 【2019.09.15】2019icpc上海网络赛
  8. RandomAccess接口
  9. Linux下gcc/g++、make和cmake的区别
  10. easyexcel多个sheet导入_Easypoi实现excel多sheet表导入导出功能
  11. 阿里云oss Referer设置
  12. GBK 汉字编码转换
  13. 网络术语大扫盲2007版
  14. 桌面窗口管理器dwm.exe内存过高,intel核显内存泄漏问题(附核显升级链接)
  15. Asp.Net Core MVC 使用Aspose.Cells从Excel获取数据
  16. Swoole 基础入门
  17. Git分布式版本管理工具
  18. 计算机视觉就业怎么样?好找工作吗?
  19. dialog dismiss时键盘不消失的问题。
  20. 液晶屏工艺中的封口抹平和端口丝印

热门文章

  1. Webdriver使用Chrome模拟手机浏览器测试移动版网站
  2. 软件开发需要重视对异常的处理
  3. 容器和云服务器集群,什么是docker集群与镜像
  4. svn authz 授权文件模版
  5. oracle open for using的用法,oracle OPEN FOR [USING] 语句
  6. murmur3 php,MySQL5.7 切不要乱射 --transaction-write-set-extraction=MURMUR32
  7. 保存模型后无法训练_如何解决推荐系统工程难题——深度学习推荐模型线上serving?...
  8. 国际智商测试皮肤软件,爆火的口服玻尿酸,是美容神器还是智商税?
  9. c语言排序系统代码怎么写,排序概述(c语言)(示例代码)
  10. Shuffle CodeForces - 1366B(思维)