标签:参数 ret ISE assert nts pre lis import argument
from inspect import signature def f(a,b,c=1):pass sig = signature(f) sig.parameters 实现一个对被装饰的函数的参数类型检查的装饰器
from inspect import signature def typeassert(*ty_args,**ty_kargs): def decorator(func): #func ->a,b #d = {‘a‘:int,‘b‘:str} sig = signature(func) btypes = sig.bind_partial(*ty_args,**ty_kargs).arguments def wrapper(*args,**kargs): #arg in d,instance(arg,d[arg]) for name, obj in sig.bind(*ty_args,**ty_kargs).arguments: if name in btypes: if not instance(obj,btyes[name]): raise TypeError(‘"%s" must be "%s"‘ %(name,btyes[name])) return func(*args,**kargs) return wrapper return decorator @typeassert(int,str,list)
标签:参数 ret ISE assert nts pre lis import argument
原文地址:https://www.cnblogs.com/bailo/p/9154386.html