练习25(函数名运用,globals(),locals(),闭包)

练习25


# def func():
#     print(666)

# 1. 函数名就是函数的内存地址
# print(func)

# 2. 函数名可以作为变量
# a = 2
# b = a
# c = b
# print(c)

# def func1():
#     print(666)
# f1 = func1
# f2 = f1
# f2()

# 3. 函数名可以作为函数的参数
# def func1():
#     print(666)
#
# def func2(x):
#     print(x) # <function func1 at 0x00000251626B8F70>
#     x()
#
# # a = 'taibai'
# # func2(a)
# print(func1) # <function func1 at 0x00000251626B8F70>

# def func():
#     print(666)
#
# def func1():
#     func()
#
# def func2(x):
#     x() # func1()
#
# func2(func1)


# 4. 函数名可以当作函数的返回值

# def wrapper():
#     def inner():
#         print('inner    ')
#         return 666
#     return  inner

# ret = wrapper() # inner
# a=ret() # inner()
# print(a)

# def func2():
#     print('in func2')
#
# def func3(x): # x = func2
#     print('in func3')
#     return x # func2
# f1 = func3(func2) # func2
# f1() # func2()

# 5. 函数名可以作为容器类型的元素

# a = 6
# b = 4
# c = 5
# l1 = [a,b,c]
# print(l1)

# def func1():
#     print('in func1')
#
# def func2():
#     print('in func2')
#
# def func3():
#     print('in func3')
#
# def func4():
#     print('in func4')
#
# l1 = [func1,func2,func3,func4]
# for i in l1:
#     i()

# 向上面的函数名这种,叫第一类对象


# globals() # locals() 俩个内置函数
# globals() # 返回全局变量的一个字典
# locals() # 返回 当前位置 局部变量的字典

# def func1():
#     a = 1
#     b = 3
#     # print(globals())
#     # print(locals())
#     def inner():
#         c = 5
#         d = 6
#         print(globals())
#         print(locals())
#     inner()
# # print(globals())
# # print(locals())
# func1()

# 内层函数对外层函数的变量(非全局变量)的引用,并返回 这样就形成了闭包

# def wrapper():
#     name = 'alex'
#     def inner():
#         print(name)
#     print(inner.__closure__) # none
#     inner()
#     return inner
# wrapper()

# name = 'alex'
# def wrapper():
#     def inner():
#         print(name)
#     print(inner.__closure__)
#     inner()
#     return inner
# wrapper()

# name = 'alex'
# def wrapper(n):
#     n = 'alex'
#     def inner():
#         print(n)
#     print(inner.__closure__) # (<cell at 0x000001EA73683FD0: str object at 0x000001EA731138F0>,)
#     inner()
#     return inner
# wrapper(name)

'''
闭包作用:
    当程序执行时,遇到函数执行,它会在内存中开辟一个局部名称空间,
    如果这个函数内部形成了闭包,那么它就不会随着函数的结束而立马消失.
'''

# from urllib.request import urlopen
#
# def index():
#     url = 'http://10.130.0.112:8080/SMS/login.jsp'
#     def get():
#         return urlopen(url).read()
#     return get
# img = index() # get
# content = img() # get()
# print(content.decode('utf-8'))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值