1、函数的有用信息
代码1:
1 def login(username, password): 2 """ 3 此函数需要用户名,密码两个参数,完成的是登录的功能。 4 :return: True 登录成功。 False登录失败。 5 """ 6 print(login.__name__) 7 print(login.__doc__) 8 print(‘登录成功...‘) 9 return True 10 11 print(login.__name__) 12 print(login.__doc__)
函数的有用信息__代码1:
结果:
D:\Python36\python.exe "E:/Python/课堂视频/day12视频与课堂笔记/day12课堂笔记/day12/03 函数的有用信息.py" login 此函数需要用户名,密码两个参数,完成的是登录的功能。 :return: True 登录成功。 False登录失败。 Process finished with exit code 0
示例1结果
代码2:
1 def wrapper(f): 2 def inner(*args, **kwargs): 3 print(f.__name__) 4 print(f.__doc__) 5 """执行函数之前的操作""" 6 ret = f(*args, **kwargs) 7 """执行函数之后的操作""" 8 return ret 9 return inner 10 11 @wrapper 12 def login(username, password): 13 """ 14 此函数需要用户名,密码两个参数,完成的是登录的功能。 15 :return: True 登录成功。 False登录失败。 16 """ 17 print(‘登录成功...‘) 18 return True 19 20 print(login.__name__) 21 print(login.__doc__) 22 login(1, 2)
函数的有用信息__代码2:
结果:
D:\Python36\python.exe "E:/Python/课堂视频/day12视频与课堂笔记/day12课堂笔记/day12/03 函数的有用信息.py" inner None login 此函数需要用户名,密码两个参数,完成的是登录的功能。 :return: True 登录成功。 False登录失败。 登录成功... Process finished with exit code 0
示例2结果
代码3:
1 from functools import wraps 2 3 def wrapper(f): 4 @wraps(f) 5 def inner(*args, **kwargs): 6 """执行函数之前的操作""" 7 ret = f(*args, **kwargs) 8 """执行函数之后的操作""" 9 return ret 10 return inner 11 12 @wrapper 13 def login(username, password): 14 """ 15 此函数需要用户名,密码两个参数,完成的是登录的功能。 16 :return: True 登录成功。 False登录失败。 17 """ 18 a = 2 19 c = 4 20 print(‘登录成功...‘) 21 return True 22 23 print(login.__name__) 24 print(login.__doc__) 25 for i in login.__dict__: 26 print(i) 27 print(dir(login))
函数的有用信息__代码3
结果:
D:\Python36\python.exe "E:/Python/课堂视频/day12视频与课堂笔记/day12课堂笔记/day12/03 函数的有用信息.py" login 此函数需要用户名,密码两个参数,完成的是登录的功能。 :return: True 登录成功。 False登录失败。 __wrapped__ [‘__annotations__‘, ‘__call__‘, ‘__class__‘, ‘__closure__‘, ‘__code__‘, ‘__defaults__‘, ‘__delattr__‘, ‘__dict__‘, ‘__dir__‘, ‘__doc__‘, ‘__eq__‘, ‘__format__‘, ‘__ge__‘, ‘__get__‘, ‘__getattribute__‘, ‘__globals__‘, ‘__gt__‘, ‘__hash__‘, ‘__init__‘, ‘__init_subclass__‘, ‘__kwdefaults__‘, ‘__le__‘, ‘__lt__‘, ‘__module__‘, ‘__name__‘, ‘__ne__‘, ‘__new__‘, ‘__qualname__‘, ‘__reduce__‘, ‘__reduce_ex__‘, ‘__repr__‘, ‘__setattr__‘, ‘__sizeof__‘, ‘__str__‘, ‘__subclasshook__‘, ‘__wrapped__‘] Process finished with exit code 0
示例3结果
2、带参数的装饰器
代码:
1 def timer_out(flag1): # falg1 = flag 2 def timer(f): 3 def inner(*args, **kwargs): 4 if flag1: 5 start_time = time.time() 6 time.sleep(0.3) 7 ret = f(*args, **kwargs) 8 end_time = time.time() 9 print(‘执行效率%s‘ % (end_time - start_time)) 10 return ret 11 else: 12 ret = f(*args, **kwargs) 13 return ret 14 15 return inner 16 17 return timer 18 19 20 flag = False 21 22 23 @timer_out(flag) # 1,步 timer_out(flag) == timer 2,@与timer结合,变成你熟悉的装饰器 @timer 24 def func1(): 25 print(111) 26 27 28 @timer_out(flag) 29 def func2(): 30 print(222) 31 32 33 @timer_out(flag) 34 def func3(): 35 print(333) 36 37 38 func1() 39 func2() 40 func3()
带参数的装饰器__示例:
结果:
D:\Python36\python.exe "E:/Python/课堂视频/day12视频与课堂笔记/day12课堂笔记/day12/04 带参数的装饰器.py" 111 222 333 Process finished with exit code 0
示例结果
局部只能引用全局的变量,不能修改,如果要修改,global。
1 count = 1 2 3 def func4(): 4 count = count + 1 5 print(count) 6 7 func4()
global
结果:
D:\Python36\python.exe "E:/Python/课堂视频/day12视频与课堂笔记/day12课堂笔记/day12/04 带参数的装饰器.py" Traceback (most recent call last): File "E:/Python/课堂视频/day12视频与课堂笔记/day12课堂笔记/day12/04 带参数的装饰器.py", line 54, in <module> func4() File "E:/Python/课堂视频/day12视频与课堂笔记/day12课堂笔记/day12/04 带参数的装饰器.py", line 51, in func4 count = count + 1 UnboundLocalError: local variable ‘count‘ referenced before assignment Process finished with exit code 1
示例结果
子函数只能引用父函数的变量,不能修改,如果要修改,nonlocal。
1 def func4(): 2 3 count = 3 4 def inner(): 5 nonlocal count 6 count = count + 1 7 print(count) 8 func4()
nonlocal
结果:
D:\Python36\python.exe "E:/Python/课堂视频/day12视频与课堂笔记/day12课堂笔记/day12/04 带参数的装饰器.py" Process finished with exit code 0
示例结果
3、多个装饰器装饰一个函数
1 def wrapper1(func): # func = f函数名 2 def inner1(): 3 print(‘wrapper1 ,before func‘) # 2 4 func() 5 print(‘wrapper1 ,after func‘) # 4 6 return inner1 7 8 9 def wrapper2(func): # func = inner1 10 def inner2(): 11 print(‘wrapper2 ,before func‘) # 1 12 func() # inner1() 13 print(‘wrapper2 ,after func‘) # 5 14 return inner2 15 16 @wrapper2 # f = wrapper2(f) 里面的f新变量 = inner1 外面的f最新变量 = inner2 17 @wrapper1 # f = wrapper1(f) 里面的f函数名 外面的f新变量=inner1 18 def f(): 19 print(‘in f‘) # 3 20 21 f()
示例
结果:
D:\Python36\python.exe "E:/Python/课堂视频/day12视频与课堂笔记/day12课堂笔记/day12/05 多个装饰器装饰一个函数.py" wrapper2 ,before func wrapper1 ,before func in f wrapper1 ,after func wrapper2 ,after func Process finished with exit code 0
示例结果
原文地址:https://www.cnblogs.com/ZN-225/p/9124585.html
时间: 2024-10-13 00:06:42