pyton whole stack development (92 days) - adhere to the first 10 days

Today content of the work has been completed, so the learning company in the afternoon, the scope of the problems encountered closures always wanted to do not understand, so he asked us to develop also saw a long time to understand (because he is to get java), then my heart I think that it is not all men are clever, others become good now because Jing Xiaxin to study improved. So, if you want to switch to suffering heart and calm but firm.

 

A variable closure scope

  python closure is if an internal function, the external scope (but not in the global scope) variables are referenced, then the internal function is considered to be closure

def outer():
    x=8
    def inner(y):
        return x + y
    return inner

  The following should be a lot of people have seen, but in my first look at the time I thought it was 3, 9, 9 but the program is run, then debug like a long time came to understand

  

def count():
    fs = []
    for i in range(1,4):
        def fn():
            return i*i
        fs.append(fn)
    return fs
f1,f2,f3 = count()
print(f1()) #9
print(f2()) #9
print(f3()) #9

Because it is fs saved fn function, return i * i i where the use of external functions for loop inside i, when calling fn function, for i i already inside the loop becomes 3, fn when the function to be performed find the variable i is i = 3, so we are all 9

Want to top the results into 1,4,9 can use the following method:

DEF COUNT ():
     DEF F (J):
         DEF G ():
             return J * J
         return G 
    FS = []
     for I in Range (. 1,. 4 ): 
        fs.append (F (I)) # F (I) immediately executed, so the current value of i is passed F (), each time the contents of the address will point i g of a 
    return FS 

F1, F2, F3 = COUNT ()
 Print (F1 ()) # . 1 
Print (F2 ( )) # . 4 
Print (F3 ()) # 922: 40: 53 is

The following code error: UnboundLocalError: local variable 'a' referenced before assignment

a = 10
def bar():
    print(a)
    a = 'in bar'
bar()

The reason given is because python compile time find a local variable is like the definition of a =, no assignment.

In the above function bar at the time of execution of the first statement, why not visit it a global variable that is defined Python syntax, when there is an assignment statement in the body of the function, compile time that the definition of a local variable , thus ensuring the encapsulation function

Two, global

I do not want the value of the previous error can use the global keyword

a = 10
def bar():
    global a
    a = 'in bar'
bar()
print(a) #in bar

But the use of such codes have to be careful, he can easily change the value of a global variable

Three, nonlocal

Closure

def fn():
    count = 8
    def inner(dt=0):
        r =count +dt
        print(r) #8
    return inner
fn()()

If the above code changes a little bit he would report the above error: UnboundLocalError: local variable 'a' referenced before assignment, and principle is the same as before

def fn():
    count = 8
    def inner(dt=0):
        count+=dt
        print(count) 
    return inner
fn()() #会报错

If you want to not being given the above code you can use nonlocal keyword as follows:

def fn():
    count = 8
    def inner(dt=0):
        nonlocal count
        count =count +dt
        print(count)
    return inner
fn()() #8

nonlocal keyword use that variable when no internal function, external function may be to look for variables.

But it is worth noting that the use of nonlocal keyword, variable internal function changes the variables of the outer function will change as follows:

name = 'global'
def test():
    name = 'local'

    def inner_test():
        nonlocal name
        # global name

        name = name + '变量'
        return name

    print(name) #'local'+'变量'
    return inner_test()

print(name) #'global'
print(test())
print(name) #'global'
DEF funX (): 
    X =. 5
     DEF funy (): 
        nonlocal X 
        X + =. 1
         return X
     return funy 
A = funX ()
 Print (A ()) # . 6 
Print (A ()) # . 7 because x + = 1 has already x is changed external function. 6 
Print (a ()) # . 8

So I feel global and nonlocal or less good

                        ------ Schrodinger's a salted fish

 

 

Guess you like

Origin www.cnblogs.com/venvive/p/11291742.html
Recommended