02,函数名的应用(第一对象)。
# 1,直接打印函数名得到的是函数的内存地址 <function func1 at 0x0000000002876B70>
# print(func1)
#2,函数名可以赋值运算。
# def func1():
# print(666)
# f1 = func1
# f1()
# 3, 函数名可以作为函数的参数。
# def func1():
# print(666)
#
# def func2(x):
# x()
# print(555)
# func2(func1)
# 4,函数名可以作为容器类数据类型的元素。
# def func1():
# print(666)
#
# def func2():
# print(222)
#
# def func3():
# print(111)
#
# def func4():
# print(777)
# l1 = [func1, func2, func3, func4]
# for i in l1:
# i()
# dic1 = {
# 1:func1,
# 2:func2,
# 3:func3,
# 4:func4,
# }
# dic1[1]()
# 5,函数名可以当做函数的返回值
def func1():
print(666)
def func2(x): # x = func1
print(222)
return x
ret = func2(func1)
ret()
03,闭包。
# 内层函数对外层函数非全局变量的引用就叫闭包
#判断是不是闭包 函数名.__closure__
# 返回的None则不是闭包,返回的是cell.... 则是闭包
# 闭包有什么用?
#当执行一个函数时,如果解释器判断此函数内部闭包存在,这样
#Python就一个机制,闭包的所在的临时名称空间不会随着函数的执行完毕而消失。
# def func1():
# name = '老男孩'
#
# def inner():
# print(name)
# inner()
# print(inner.__closure__) # <cell at 0x000000000282A768: str object at 0x0000000002856E10>
#
# func1()
# def func1():
# global name
# name = '老男孩'
# def inner():
# print(name)
# inner()
# print(inner.__closure__) # None
# func1()
# def func1(x): # x = '老男孩'
# def inner():
# print(x)
# inner()
# print(inner.__closure__) #(<cell at 0x00000208229EEE88: str object at 0x0000020822D436F0>,)
# name = '老男孩'
# func1(name)
def func():
def func1():
name ="老男孩"
def func2():
nonlocal name
name = "alex"
def func3():
global name
name = "太白"
name = "日天"
func1()
print(name) # 1 日天
func2()
print(name) # 2,alex
func3()
print(name) # 3,alex
func()
print(name) # 4,太白
# from urllib.request import urlopen
#
# def index():
# url = "http://www.xiaohua100.cn/index.html"
# def get():
# return urlopen(url).read()
# return get
#
# xiaohua = index()
# content = xiaohua()
#
# print(content)
04,装饰器。
#装饰器功能:在不改变原函数的基础上,为原函数增加一些额外的功能,log,登录注册,等等.
测试程序的效率
import time
'''第一版本,测试函数low'''
# def login():
# time.sleep(0.3)
# print('洗洗更健康...')
#
# def timmer():
# start_time = time.time()
# login()
# end_time = time.time()
# print('此函数的执行时间%s' % (end_time - start_time))
# timmer()
# 改变了我原来执行函数的执行方式,不好
# def login():
# time.sleep(0.3)
# print('洗洗更健康...')
# # login()
#
# def register():
# time.sleep(0.4)
# print('洗洗更健康22222...')
# # register()
# def timmer(f):
# start_time = time.time()
# f()
# end_time = time.time()
# print('此函数的执行时间%s' % (end_time - start_time))
#
# timmer(login)
# timmer(register)
# 虽然执行函数的方式已经无限接近于原方式,但是更麻烦了,增加了两步代码。改
# def login():
# time.sleep(0.3)
# print('洗洗更健康...')
# # login()
#
# def timmer(f):
# start_time = time.time()
# f()
# end_time = time.time()
# print('此函数的执行时间%s' % (end_time - start_time))
#
# f1 = login # 将login函数名给了f1
# login = timmer # 将timmer函数名给了login
# login(f1) # timmer(login)
# 初级装饰器
# def login():
# time.sleep(0.3)
# print('洗洗更健康...')
# # login()
#
# def timmer(f): # f = login函数名
#
# def inner():
# start_time = time.time()
# f() # login()
# end_time = time.time()
# print('此函数的执行时间%s' % (end_time - start_time))
# return inner
#
# login = timmer(login) # inner 此login是新变量
# login() # inner()
# name = 'alex'
# name = '老男孩'
# 简单版装饰器 语法糖
# def timmer(f): # f = login函数名
# def inner():
# start_time = time.time()
# f() # login()
# end_time = time.time()
# print('此函数的执行时间%s' % (end_time - start_time))
# return inner
#
# @timmer # login = timmer(login) # inner 此login是新变量
# def login():
# time.sleep(0.3)
# print('洗洗更健康...')
# login()
# @timmer # register = timmer(register)
# def register():
# time.sleep(0.2)
# print('洗洗更健康22...')