装饰器多层语法糖嵌套
小练习:
# # 先验证登录
# # 再验证 输入的金额 --- 符合数字 / 余额充足
#
# # 取款函数里面
# def get_balance():
# # 校验登录
# # 校验金额 符合数字 / 余额充足 --- 把金额通过装饰器 返回来
# # 拿着你的金额进行提款
use_data = {'username': 'dream', 'password': '521'}
bank_data = {'dream': {'pay_pwd': '521', 'balance': 1000}}
def login_auth(func):
def inner(*args,**kwargs):
if use_data['username']!='dream' and use_data['password']!='521':
return False,f'用户名或密码错误!'
else:
return func(*args,**kwargs)
return inner
def get_balance_auth(func):
def inner(*args,**kwargs):
if bank_data['dream']['pay_pwd'] != '521':
return False,f'取款密码错误!'
get_balance_input=input('请输入取款金额:').strip()
if get_balance_input.isdigit():
get_balance_input=int(get_balance_input)
if 0<get_balance_input<=bank_data['dream']['balance']:
return func(balance=get_balance_input,*args,**kwargs)
else:
return False,f'余额不足!'
return inner
@login_auth
@get_balance_auth
def get_balance(*args,**kwargs):
# print(kwargs)
bank_data['dream']['balance']-=kwargs.get('balance')
bank_data['dream']['balance']=bank_data['dream']['balance']
# print(bank_data)
return f"取款金额为{kwargs.get('balance')},还剩下{bank_data['dream']['balance']}"
print(get_balance())
有参装饰器模板
"""有参装饰器模板"""
def wrapper_func(tag):
def wrapper(func):
def inner(*args,**kwargs):
#tag 表明当前需要处理的使用函数的逻辑
#存钱--不需要校验提款密码
#取钱--需要校验提款密码
if tag==None:
...
#正常函数内需要的逻辑
return func(*args,**kwargs)
return inner
return wrapper
wrapper_func(tag=None)
def balance():
...
练习
- use_data = {'username': 'dream', 'password': '521'}
bank_data = {'dream': {'pay_pwd': '521', 'balance': 1000}} - 取款-----校验登录-----校验取款密码
- 存款------校验登录-----不需要校验密码
use_data = {'username': 'dream', 'password': '521'}
bank_data = {'dream': {'pay_pwd': '521', 'balance': 1000}}
def check_balance(tag_fun=None,tag=None):
if tag_fun=='login':
def login_auth(func):
def inner(*args,**kwargs):
if use_data['username']!='dream' and use_data['password']!='521':
return False,f"用户名或密码错误!"
else:
return func(*args,**kwargs)
return inner
return login_auth
elif tag_fun=='balance':
def balance_auth(func):
def inner(*args,**kwargs):
#存款
balance_input=input('请输入金额:')
if not balance_input.isdigit():
return False, f'输入有误!'
balance_input=int(balance_input)
if tag!='add_balance':
pay_password_input=input('请输入取款密码:').strip()
if pay_password_input!=bank_data['dream']['pay_pwd']:
return False,f'取款密码有误!'
if balance_input>bank_data['dream']['balance']:
return False,f'余额不足!'
return func(balance=balance_input,*args,**kwargs)
return inner
return balance_auth
@check_balance(tag_fun='login')
@check_balance(tag_fun='balance',tag='get_balance')
#取款
def get_balance(*args,**kwargs):
bank_data['dream']['balance']-=kwargs.get('balance')
bank_data['dream']['balance']=bank_data['dream']['balance']
return f"取款{kwargs.get('balance')}剩余{bank_data['dream']['balance']}"
print(get_balance())
#存款
@check_balance(tag_fun='login')
@check_balance(tag_fun='balance',tag='add_balance')
def add_balance(*args,**kwargs):
bank_data['dream']['balance'] += kwargs.get('balance')
bank_data['dream']['balance'] = bank_data['dream']['balance']
return f"村款{kwargs.get('balance')}剩余{bank_data['dream']['balance']}"
# print(add_balance())
标签:return,dream,多层,语法,嵌套,tag,balance,data,def
From: https://www.cnblogs.com/suyihang/p/17899302.html