None
默认情况下,每个参数都需要某种装饰方法。如果只是关于Python 3,那么
inspect.signature
可用于:
def function_arguments_default_to_None(func):
# Get the current function signature
sig = inspect.signature(func)
# Create a list of the parameters with an default of None but otherwise
# identical to the original parameters
newparams = [param.replace(default=None) for param in sig.parameters.values()]
# Create a new signature based on the parameters with "None" default.
newsig = sig.replace(parameters=newparams)
def inner(*args, **kwargs):
# Bind the passed in arguments (positional and named) to the changed
# signature and pass them into the function.
arguments = newsig.bind(*args, **kwargs)
arguments.apply_defaults()
return func(**arguments.arguments)
return inner
@function_arguments_default_to_None
def x(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z):
print(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z)
x()
# None None None None None None None None None None None None None None
# None None None None None None None None None None None None
x(2)
# 2 None None None None None None None None None None None None None
# None None None None None None None None None None None None
x(q=3)
# None None None None None None None None None None None None None None
# None None 3 None None None None None None None None None
但我怀疑可能有更好的方法来解决问题或完全避免问题。