time库:
import time
ret1=time.ctime()
ret2=time.time()
print(ret1)
time.sleep(10)#延时
print(ret2)
#另一种写法
# from time import ctime,time,sleep
# from time import *
# ctime()
# time()
不定长参数
def wrapper(func):
def inner(*args,**kwargs):
print('登陆验证')
func(*args,**kwargs)
return inner
被装饰的函数存在参数
def wrapper(func):
def inner(total):
print('登陆验证')
func(total)
return inner
@wrapper
def cost(total):
print('总付款金额:%s'%total)
print('总付款金额:',total)
if __name__ == '__main__':
cost(10)
装饰器工厂
import time
def warpper1(func):
def inner():
print('登陆验证')
func()
return inner
def warpper2(func):
def inner():
print('登陆验证')
func()
return inner
def create(choose):
'''
这个函数创造的是装饰器,根据用户的需求,返回不同的装饰器
:param choose: 这个参数控制的是装饰器的生成种类
:return: 返回的是装饰器对象
'''
def warpper(func):
def inner():
if choose=='第一种':
print('登陆验证',time.ctime())
elif choose=='第二种':
print('登陆验证', time.time())
else:
print('登陆失败')
func()
return inner
return warpper
@create('第一种')
def cost():
print('总付款金额')
if __name__ == '__main__':
cost()