XYxe
2017-06-20 13:13:30 +08:00
一个比较粗略的实现:
def not_null(*varnames):
def outer(func):
def inner(*args, **kwargs):
args_name = func.__code__.co_varnames
args_cnt = func.__code__.co_argcount # 没用到
flag = True
for name in varnames: # 这里还要考虑 varnames 中有 args 中没有的参数
index = args_name.index(name) # 还可以加上考虑 kwargs 参数
flag = flag and (args[index] is not None)
if not flag:
raise TypeError('something is None')
result = func(*args, **kwargs)
return result
return inner
return outer