from inspect import signature#python3才有的模块
def typeassert(*args,**kwargs):
def decorator(fun):
sig=signature(fun)
btypes=sig.bind_partial(*args,**kwargs).arguments
def wrapper(*funargs,**funkwargs):
for name,stype in sig.bind_partial(*funargs,**funkwargs).arguments.items():
if name in btypes:
if not isinstance(stype,btypes[name]):
raise TypeError("'%s' must be '%s'"%(name,btypes[name]))
return fun(*funargs,**funkwargs)
return wrapper
return decorator @typeassert(int,str)
def myFun(a,b):
print(a,b)
myFun(,"")
05-11 21:49