翻译完成-20180208 Python Global, Local and Nonlocal variables (With Examples)

This commit is contained in:
Flowsnow 2018-02-11 16:47:34 +08:00
parent 27637d7c70
commit 812ed9bb90
2 changed files with 187 additions and 201 deletions

View File

@ -1,201 +0,0 @@
translating by Flowsnow
Python Global, Local and Nonlocal variables (With Examples)
======
### Global Variables
In Python, a variable declared outside of the function or in global scope is known as global variable. This means, global variable can be accessed inside or outside of the function.
Let's see an example on how a global variable is created in Python.
#### Example 1: Create a Global Variable
```
x = "global"
def foo():
print("x inside :", x)
foo()
print("x outside:", x)
```
When we run the code, the will output be:
```
x inside : global
x outside: global
```
In above code, we created x as a global variable and defined a `foo()` to print the global variable x. Finally, we call the `foo()` which will print the value of x.
What if you want to change value of x inside a function?
```
x = "global"
def foo():
x = x * 2
print(x)
foo()
```
When we run the code, the will output be:
```
UnboundLocalError: local variable 'x' referenced before assignment
```
The output shows an error because Python treats x as a local variable and x is also not defined inside `foo()`.
To make this work we use `global` keyword, to learn more visit [Python Global Keyword][1].
### Local Variables
A variable declared inside the function's body or in the local scope is known as local variable.
#### Example 2: Accessing local variable outside the scope
```
def foo():
y = "local"
foo()
print(y)
```
When we run the code, the will output be:
```
NameError: name 'y' is not defined
```
The output shows an error, because we are trying to access a local variable y in a global scope whereas the local variable only works inside `foo() `or local scope.
Let's see an example on how a local variable is created in Python.
#### Example 3: Create a Local Variable
Normally, we declare a variable inside the function to create a local variable.
```
def foo():
y = "local"
print(y)
foo()
```
When we run the code, it will output:
```
local
```
Let's take a look to the earlier problem where x was a global variable and we wanted to modify x inside `foo()`.
### Global and local variables
Here, we will show how to use global variables and local variables in the same code.
#### Example 4: Using Global and Local variables in same code
```
x = "global"
def foo():
global x
y = "local"
x = x * 2
print(x)
print(y)
foo()
```
When we run the code, the will output be:
```
global global
local
```
In the above code, we declare x as a global and y as a local variable in the `foo()`. Then, we use multiplication operator `*` to modify the global variable x and we print both x and y.
After calling the `foo()`, the value of x becomes `global global` because we used the `x * 2` to print two times `global`. After that, we print the value of local variable y i.e `local`.
#### Example 5: Global variable and Local variable with same name
```
x = 5
def foo():
x = 10
print("local x:", x)
foo()
print("global x:", x)
```
When we run the code, the will output be:
```
local x: 10
global x: 5
```
In above code, we used same name x for both global variable and local variable. We get different result when we print same variable because the variable is declared in both scopes, i.e. the local scope inside `foo()` and global scope outside `foo()`.
When we print the variable inside the `foo()` it outputs `local x: 10`, this is called local scope of variable.
Similarly, when we print the variable outside the `foo()`, it outputs `global x: 5`, this is called global scope of variable.
### Nonlocal Variables
Nonlocal variable are used in nested function whose local scope is not defined. This means, the variable can be neither in the local nor the global scope.
Let's see an example on how a global variable is created in Python.
We use `nonlocal` keyword to create nonlocal variable.
#### Example 6: Create a nonlocal variable
```
def outer():
x = "local"
def inner():
nonlocal x
x = "nonlocal"
print("inner:", x)
inner()
print("outer:", x)
outer()
```
When we run the code, the will output be:
```
inner: nonlocal
outer: nonlocal
```
In the above code there is a nested function `inner()`. We use `nonlocal` keyword to create nonlocal variable. The `inner()` function is defined in the scope of another function `outer()`.
Note : If we change value of nonlocal variable, the changes appears in the local variable.
--------------------------------------------------------------------------------
via: https://www.programiz.com/python-programming/global-local-nonlocal-variables
作者:[programiz][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.programiz.com/
[1]:https://www.programiz.com/python-programming/global-keyword

View File

@ -0,0 +1,187 @@
Python全局局部和非局部变量带示例
======
### 全局变量
在Python中在函数之外或在全局范围内声明的变量被称为全局变量。 这意味着,全局变量可以在函数内部或外部访问。
我们来看一个关于如何在Python中创建一个全局变量的示例。
#### 示例1创建全局变量
```python
x = "global"
def foo():
print("x inside :", x)
foo()
print("x outside:", x)
```
当我们运行代码时,将会输出:
```
x inside : global
x outside: global
```
在上面的代码中我们创建了x作为全局变量并定义了一个`foo()`来打印全局变量x。 最后,我们调用`foo()`来打印x的值。
倘若你想改变一个函数内的x的值该怎么办
```python
x = "global"
def foo():
x = x * 2
print(x)
foo()
```
当我们运行代码时,将会输出:
```
UnboundLocalError: local variable 'x' referenced before assignment
```
输出显示一个错误因为Python将x视为局部变量x也没有`foo()`内部定义。
为了运行正常,我们使用`global`关键字,访问[PythonGlobal关键字][1]以便了解更多。
### 局部变量
在函数体内或局部作用域内声明的变量称为局部变量。
#### 示例2访问作用域外的局部变量
```python
def foo():
y = "local"
foo()
print(y)
```
当我们运行代码时,将会输出:
```
NameError: name 'y' is not defined
```
输出显示了一个错误因为我们试图在全局范围内访问局部变量y而局部变量只能在`foo() `函数内部或局部作用域内有效。
我们来看一个关于如何在Python中创建一个局部变量的例子。
#### 示例3创建一个局部变量
通常,我们在函数内声明一个变量来创建一个局部变量。
```python
def foo():
y = "local"
print(y)
foo()
```
当我们运行代码时,将会输出:
```
local
```
让我们来看看前面的问题其中x是一个全局变量我们想修改`foo()`内部的x。
### 全局变量和局部变量
在这里,我们将展示如何在同一份代码中使用全局变量和局部变量。
#### 示例4在同一份代码中使用全局变量和局部变量
```python
x = "global"
def foo():
global x
y = "local"
x = x * 2
print(x)
print(y)
foo()
```
当我们运行代码时将会输出译者注原文中输出结果的两个global有空格正确的是没有空格
```
globalglobal
local
```
在上面的代码中我们将x声明为全局变量将y声明为`foo()`中的局部变量。 然后,我们使用乘法运算符`*`来修改全局变量x并打印x和y。
在调用`foo()`之后x的值变成`globalglobal`了(译者注:原文同样有空格,正确的是没有空格),因为我们使用`x * 2`打印两次`global`。 之后我们打印局部变量y的值即`local`。
#### 示例5具有相同名称的全局变量和局部变量
```python
x = 5
def foo():
x = 10
print("local x:", x)
foo()
print("global x:", x)
```
当我们运行代码时,将会输出:
```
local x: 10
global x: 5
```
在上面的代码中我们对全局变量和局部变量使用了相同的名称x。 当我们打印相同的变量时却得到了不同的结果,因为这两个作用域内都声明了变量,即`foo()`内部的局部作用域和`foo()`外面的全局作用域。
当我们在`foo()`内部打印变量时,它输出`local x: 10`,这被称为变量的局部作用域。
同样,当我们在`foo()`外部打印变量时,它输出`global x: 5`,这被称为变量的全局作用域。
### 非局部变量
非局部变量用于局部作用域未定义的嵌套函数。 这意味着,变量既不能在局部也不能在全局范围内。
我们来看一个关于如何在Python中创建一个非局部变量的例子。译者注原文为创建全局变量疑为笔误
我们使用`nonlocal`关键字来创建非局部变量。
#### 例6创建一个非局部变量
```python
def outer():
x = "local"
def inner():
nonlocal x
x = "nonlocal"
print("inner:", x)
inner()
print("outer:", x)
outer()
```
当我们运行代码时,将会输出:
```
inner: nonlocal
outer: nonlocal
```
在上面的代码中有一个嵌套函数`inner()`。 我们使用`nonlocal`关键字来创建非局部变量。`inner()`函数是在另一个函数`outer()`的作用域中定义的。
注意:如果我们改变非局部变量的值,那么变化就会出现在局部变量中。
--------------------------------------------------------------------------------
via: https://www.programiz.com/python-programming/global-local-nonlocal-variables
作者:[programiz][a]
译者:[Flowsnow](https://github.com/Flowsnow)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.programiz.com/
[1]:https://www.programiz.com/python-programming/global-keyword