def type_inspect(*ty_args, **ty_kwargs):
def decorator(func):
func_sig = inspect.Signature(func)
ba = func_sig.bind_partial(*ty_args, **ty_kwargs)
type_dict = ba.arguments
def wrap(*args, **kwargs):
for name, obj in func_sig.bind(*args, **kwargs).arguments.items():
type_ = type_dict.get(name)
if not isinstance(obj, type_):
raise TypeError('%s must be %s' % (name, type_))
return func(*args, **kwargs)
return wrap
return decorator
if __name__ == '__main__':
@type_inspect(int, list, str)
def f(a, b, c):
pass
我写了一个type_inspect函数,功能就是生产装饰器,并且给装饰器修饰的函数检查数据类型
运行了一下报错:
怎么修改呢?谢谢