在我的第一个答案被删除后,这里是更新版本:
我试图使用Bigbob556677的非常好的答案,但对我来说,它与**kwargs
不起作用,所以我进行了编辑并将其放在了Gist中,这里是https://gist.github.com/devTechi/6e633ded72cc83637f34b1a3f4a96984(代码也在下面)
我没有测试过只使用*args
,但使用(我发布了更多或更少的Gist链接)**kwargs
效果很好。
def not_none(nullable_parameters=None):
def the_actual_test(f, expected_args_with_given, allowed_nullable_args):
has_none = False
bad_parameters = []
for key, value in expected_args_with_given.items():
if (value is None and nullable_parameters is None) or \
(value is None and key not in nullable_parameters):
bad_parameters.append(key)
has_none = True
if has_none:
raise ValueError("[Function '{}' of '{}'] - IMPORTANT: Parameters '{}' cannot be None. ".format(f.__name__, f.__module__, bad_parameters))
def not_null_decorator(original_func):
import inspect
has_self = False
argspec = inspect.getargspec(original_func)
if 'self' in argspec.args:
argnames = argspec.args[1:]
has_self = True
else:
argnames = argspec.args
args_dict = dict.fromkeys(argnames)
def get_args(*args, **kwargs):
for arg in args:
if arg in args_dict.keys():
args_dict[arg] = arg
for key, value in kwargs.items():
if key in args_dict.keys():
args_dict[key] = value
return args_dict
def wrapper_with_self(self, *args, **kwargs):
the_actual_test(original_func, get_args(*args, **kwargs), nullable_parameters)
return original_func(self, *args, **kwargs)
def wrapper(*args, **kwargs):
the_actual_test(original_func, get_args(*args, **kwargs), nullable_parameters)
return original_func(*args, **kwargs)
if has_self:
return wrapper_with_self
else:
return wrapper
return not_null_decorator
使用方法:
from .nullable_decorator import not_none
@not_none(nullable_parameters=["nullable_arg1", "nullable_arg2"])
def some_function(self, nullable_arg1=None, nullable_arg2=None, non_nullable_arg1=None):
pass
@not_none()
def some_other_function(self, non_nullable_arg1=None, non_nullable_arg2=None):
pass
None
。 - Ignacio Vazquez-Abramsclean()
方法)。 - Simeon Visser