python class finishing 13 --- anonymous function scope and function

name = 'alex'
def foo():
    name = 'jinling'
    def bar():
        print(name)
    return bar
a = foo()
print(a)

Read the above code, understand a get is a function of memory address bar, bar just want to run a (), because the bar function does not return, it returns None

name = 'alex'
def foo():
    name = 'jinling'
    def bar():
        print(name)
    return bar
a = foo()
print(a())

Second, empathy

def test1():
    print('in the test1')
def test2():
    print('in the test2')
    return test1

res = test2 ()
print(res)
a = nothing ()
print(a)

three,

def foo():
    name = 'lhf'
    def bar():
        name = 'jinling'
        def tt():
            print(name)
        return tt
    return bar
a = foo () # memory address bar to give
b = a () # tt running memory address bar to give
c = b () # tt print run Jinling
print (c) # return value of tt

The above-described operation may be foo () () () instead of

########## ########### anonymous function

lambda effect is equivalent to the following function

def cal(x):
    return x + 1

= no need (10)
print(res)

a = lambda x: x + 1 # x is the shape parameter
print(a(10))

 Anonymous functions can not have complex logic, it can only be a step in the process (the result of a return of)

name = 'alex'
f = lambda x: x + '_sb'
print(f(name))

It may be a number of parameters

a = lambda x, y, z: (x + 1, y + 1, z + 1)
print(a(1, 2, 3))

Anonymous function should not exist independently, and should not be assigned, and used in conjunction with other functions, after write -

Guess you like

Origin www.cnblogs.com/dabai123/p/11055454.html