如何一次将所有默认参数值设置为 None
How to set all default parameter values to None at once
我有一个带有 n 个参数的方法。我想将所有默认参数值设置为None
,例如:
def x(a=None,b=None,c=None.......z=None):
如果在编写方法时没有将所有参数值默认设置为None,是否有任何内置方法可以立即将所有参数值设置为None?
如果你真的想将 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
但是,由于您手动更改了签名,因此您将失去对函数的自省。
但我怀疑可能有更好的方法来解决问题或完全避免问题。
对于普通函数,您可以设置 __defaults__
:
def foo(a, b, c, d):
print (a, b, c, d)
# foo.__code__.co_varnames is ('a', 'b', 'c', 'd')
foo.__defaults__ = tuple(None for name in foo.__code__.co_varnames)
foo(b=4, d=3) # prints (None, 4, None, 3)
我有一个带有 n 个参数的方法。我想将所有默认参数值设置为None
,例如:
def x(a=None,b=None,c=None.......z=None):
如果在编写方法时没有将所有参数值默认设置为None,是否有任何内置方法可以立即将所有参数值设置为None?
如果你真的想将 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
但是,由于您手动更改了签名,因此您将失去对函数的自省。
但我怀疑可能有更好的方法来解决问题或完全避免问题。
对于普通函数,您可以设置 __defaults__
:
def foo(a, b, c, d):
print (a, b, c, d)
# foo.__code__.co_varnames is ('a', 'b', 'c', 'd')
foo.__defaults__ = tuple(None for name in foo.__code__.co_varnames)
foo(b=4, d=3) # prints (None, 4, None, 3)