# 由于语法糖@的限制,outter函数只能有一个参数,并且该才是只用来接收被装饰对象的内存地址
def outter(func):
def wrapper(*args,**kwargs):
res=func(*args,**kwargs)
return res
return wrapper
@outter
def index(x,y):
print(x,y)
# 语法糖
def auth(db_type):
def deco(func):
def wrapper(*args, **kwargs):
name = input('your name>>>: ').strip()
pwd = input('your password>>>: ').strip()
if db_type == 'file':
print('基于文件的验证')
if name == 'egon' and pwd == '123':
res = func(*args, **kwargs)
return res
else:
print('user or password error')
elif db_type == 'mysql':
print('基于mysql的验证')
else:
print('不支持该db_type')
return wrapper
return deco
@auth(db_type='file')
def index(x, y):
print('index->>%s:%s' % (x, y))
@auth(db_type='mysql')
def home(name):
print('home->>%s' % name)
# index(1, 2)
# home('egon')
有参装饰器模板
有参装饰器模板
def 有参装饰器(x,y,z):
def outter(func):
def wrapper(*args, **kwargs):
res = func(*args, **kwargs)
return res
return wrapper
return outter
@有参装饰器(1,y=2,z=3)
def 被装饰对象():
pass