python - function 12, scope

Order: local scope -> global scope
Global scope: 
Global scope: Both built-in namespaces and global namespaces belong to the global scope and can be referenced anywhere in the file
a=1
def foo():
    print(a)
    def foo2():
        print(a)
    foo2()
def foo3():
    print(a)
foo()
foo3()
View Code
Local scope: The local namespace belongs to the local scope and can only be referenced inside the function
def x1():       # todo looks inside out
    a=10
    def x2():
        a=20
        def x3():
            a=30
            print(a)
        x3()
    x2()
x1()
#Change def 
x1 ():
    a=10
    def x2():
        a=20
        def x3():
            a=30
            print(a)
        x3()
    x2()
x1()
# todo comment out a=30, because the part is from the inside to the outside, it will output 20 
# todo Similarly, if a=20 is also commented out, it will output 10
View Code

 



 


Guess you like

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