程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

5、 Python learning notes - functions - Fundamentals of functions (IV)

編輯:Python
# The scope of the function
"""
Python in , Program variables are not accessible anywhere , Access depends on where the variable is assigned .
The scope of a variable determines which part of the program can access which specific variable name .Python There are a total of 4 Kind of , Namely :
L (Local) Local scope
E (Enclosing) In a function other than a closure function
G (Global) Global scope
B (Built-in) Built in scope
With L –> E –> G –>B Rule search for , namely : I can't find , Then I will go to the part outside the part to find ( Such as closures ), If you can't find it, you will go to the whole situation , Then go to build in to find .
"""
# Four scopes
"""
1、 Location of the four scopes
2、 A global scope cannot call a local scope variable .
3、 A local scope cannot modify a global scope variable .
"""
# Example 1、 Location of the four scopes
b = int(3.14) # int() stay B(Built-in) Built in scope , also str() print() wait
g = 3 # Global variables G(Global) Global scope
def foo1():
e = 4 # E(Enclosing) Nested scope ( It can also be understood as a function outside the closure function , In function nesting Foo1 It's called a closure function ,Foo It is called a function outside the closure function )
def bar1():
l = 5 # L (Local) Local scope
# Example 2、 A global scope cannot call a local scope , The following code reports an error and catches an exception , Print exception information
def foo2():
x = 10
try:
print(x)
except Exception as e:
print(e)
# Example 3
"""
1、 A local scope can call a global scope
2、 A local scope cannot modify a global scope variable , Will report a mistake , Capture exception
Error message ‘local variable 'y' referenced before assignment’, This indicates that this variable has been called before it is created .
Because of the scope's search scope , The variable has been found in the local scope y, But it has been called before it is created , Not because of modification .
However, it also shows that the local scope cannot modify the global scope , Because you declare a variable with the same name python Think you are a local variable , Global variables cannot be manipulated at all .
3、 Declare in the local scope ( Use global) Global scope variables , You can modify .
4、 Declare nested scopes in local scope (E) Use nonlocal, You can modify
"""
# Define global variables
y = 10
# A local scope can call a global scope
def foo3():
print(y)
foo3()
# A local scope cannot modify a global scope variable
def foo4():
try:
y += 1
except Exception as e:
print(e)
else:
print(y)
foo4()
# Declare in the local scope ( Use global) Global scope variables , You can modify
def foo5():
global y
y = 5
print(y)
foo5()
# Declare nested scopes in local scope (E) Use nonlocal, You can modify
def foo6():
z = 20
def bar6():
nonlocal z
z = 15
print(z)
bar6()
print(z)
foo6()
""" summary
1、 Variable lookup order L –> E –> G –>B
2、 Only modules , class , Function to introduce a new scope
3、 For a variable , If the internal scope is declared first, the external scope will be overwritten , Do not declare direct use of , The external scope will be used
4、 The local scope is used to modify the global scope variable global, To modify a nested scope, use nonlocal
"""

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved