16.函数作用域,匿名函数

 原文:https://www.cnblogs.com/linhaifeng/articles/6113086.html#_label6

函数作用域

# def test1():
#     print('in the test1')
# def test():
#     print('in the test')
#     return test1
#
# # print(test)
# res=test()
# print(res()) #test1()

#函数的作用域只跟函数声明时定义的作用域有关,跟函数的调用位置无任何关系
# name = 'alex'
# def foo():
#     name='linhaifeng'
#     def bar():
#         # name='wupeiqi'
#         print(name)
#     return bar
# a=foo()
# print(a)
# a() #bar()

name='raitorei'
def foo():
    name='asher'
    def bar():
        name='ray'
        print(name)
        def tt():
            print(name)
        return tt
    return bar
# bar=foo()
# tt=bar()
# print(tt)
# tt()
# r1 = foo()
# r2 = r1()  # tt
# r3 = r2()
foo()()()

 匿名函数

# lambda x:x+1

# def calc(x):
#     return x+1
#
# res=calc(10)
# print(res)
# print(calc)
#
# print(lambda x:x+1)
# func=lambda x:x+1
# print(func(10))


# name='raito' #name='raito_rei'
# def change_name(x):
#     return name+'_rei'
#
# res=change_name(name)
# print(res)
#
# func=lambda x:x+'_rei'
# res=func(name)
# print('匿名函数的运行结果',res)


# func=lambda x,y,z:x+y+z
# print(func(1,2,3)) # 6

# func=lambda x,y,z:x+y+z
# print(func('raito','_rei','_ray'))#raito_rei_ray

# def test(x,y,z):
#     return x+1,y+1  #----->(x+1,y+1)

# fun = lambda x,y,z:(x+1,y+1,z+1)
# print(fun(1,2,3))#(2, 3, 4)

lambda x,y,z:(x+1,y+1,z+1)
原文地址:https://www.cnblogs.com/raitorei/p/11730861.html