全局变量和局部变量命名规则_变量范围和LEGB规则
全局變量和局部變量命名規則
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返回什么:它返回內部函數inside 。 power_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
全局變量和局部變量命名規則
總結
以上是生活随笔為你收集整理的全局变量和局部变量命名规则_变量范围和LEGB规则的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生活日历的需求分析
- 下一篇: dask 使用_在Google Clou