闭包
def fun():
a = 1
def func():
print(a)
return func
if __name__ == '__main__':
b = fun()
b()
闭包就是在函数中定义新的函数 运行顺序为赋值,然后运行到return函数 在执行func函数
简单装饰器
def funcTime(func):
print("func")
def itor():
startTime = time.perf_counter()
print("开始前")
func()
print("开始后")
endTime = time.perf_counter()
print("程序{}运行{}".format(func.__name__, (endTime - startTime) * 1000))
return itor
@funcTime
def fun():
print("hello")
if __name__ == '__main__':
fun()
运行顺序
@funcTime=>funcTime函数=>print('func')=>main函数执行=>itor()=>开始前=>fun()函数=>开始后=>结束
装饰器简单传参
def funPrin(*args, **kwargs):
for i in args:
print(i, end='')
print()
# print("{}".format(i for i in (1, 2)))
def funcTime(func):
def itor():
startTime = time.perf_counter()
func()
endTime = time.perf_counter()
print("程序{}运行{}".format(func.__name__, (endTime - startTime) * 1000))
return itor
return funcTime
@funPrin(1, 2)
def fun1():
list1 = [i for i in range(9999999)]