python - function 11, namespace

Three namespaces 
1. Built-in namespace:
The name that comes with the python interpreter, such as: print, sum, max
This space is formed when the interpreter starts
2. Global namespace: 
This space is generated when the python program is executed.
Write code at the top of each line
a=10
def foo():
    print(a)
foo()
View Code
3: Local namespace 
The name defined inside the
function takes effect when the function is called
# def foo2(a,b):
#     c=1
#     print(a,b,c)
# foo2(10,9)
View Code
Loading order of the three: built-in -> global -> local 

value order of the three: local -> global -> built-in
a=10
def foo1():
    a=20
    print(a)
foo1()
#Because there is a value locally, it is printed as 20


b=10
def foo2():
    #b=20
    print(a)
foo2()
#When the local has no value, it will find the global so it is printed as 10



#sum=10
def foo3():
    #sum=20
    print(sum)
foo3()
#When the local has no value and the global has no value, it will find the built-in
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324679143&siteId=291194637