python 动态添加装饰器_为函数动态添加装饰器和参数类型检查

为了对传入参数进行类型检查而实现的类似其它语言的静态数据类型

def type_limit(typeLimit,*returnType):def test_value_type(func):

def wrapper(*param,**kw):

length = len(typeLimit)

if length != len(param):

raise LimitError("The list of typeLimit and param must have the same length")

for index in range(length):

t = typeLimit[index]

p = param[index]

if not isinstance(p,t):

raise LimitError("The param %s should be %s,but it's %s now!"%(str(p),type(t()),type(p)))

res = func(*param,**kw)

if "returnType" in returnType:

limit = returnType["returnType"]

if not isinstance(res,limit):

raise LimitError("This function must return a value that is %s,but now it's %s"%(limit,type(res) ) )

return res

return wrapper

return test_value_type

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值