Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何一次将所有默认参数值设置为“无”_Python_Python 3.x_Function_Optional Parameters - Fatal编程技术网

Python 如何一次将所有默认参数值设置为“无”

Python 如何一次将所有默认参数值设置为“无”,python,python-3.x,function,optional-parameters,Python,Python 3.x,Function,Optional Parameters,我有一个有n个参数的方法。我想将所有默认参数值设置为None,例如: def x(a=None,b=None,c=None.......z=None): 如果写入方法时未将所有参数值设置为默认值为无,是否有任何内置方法可将所有参数值一次设置为无 如果您真的想将None添加为每个参数的默认值,则需要某种装饰方法。如果只是关于Python 3,那么可以使用: def function_arguments_default_to_None(func): # Get the current f

我有一个有n个参数的方法。我想将所有默认参数值设置为
None
,例如:

def x(a=None,b=None,c=None.......z=None): 

如果写入方法时未将所有参数值设置为默认值为无,是否有任何内置方法可将所有参数值一次设置为无

如果您真的想将
None
添加为每个参数的默认值,则需要某种装饰方法。如果只是关于Python 3,那么可以使用:

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
但是,这样您将失去对函数的内省,因为您手动更改了签名


但我怀疑可能有更好的方法来解决问题或完全避免问题。

如果你真的想在每个参数中添加
None
作为默认值,你需要某种装饰方法。如果只是关于Python 3,那么可以使用:

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
但是,这样您将失去对函数的内省,因为您手动更改了签名


但我怀疑可能有更好的方法来解决问题或完全避免问题。

对于普通函数,您可以设置:


对于普通函数,可以设置:


据我所知不是这样,但是你可以编写一个装饰程序来处理它。是的,可能有很多方法可以实现这一点,但都会带来一些“不利因素”(至少如果你不在IDE级别这样做的话),例如,这会严重限制内省。我的问题来自这个问题。我不得不写这么多次。那么更简单的方法是什么呢?如果一个函数有那么多默认值和参数,这意味着应该将这些参数封装在一个对象本身中(一个
dict
似乎是合理的),并将其传递给函数。然后,作为您的健全性检查/验证的一部分,您可以默认那里没有提供的任何内容…您甚至可以接受
**kwargs
,并在内部执行
params=dict.fromkeys('abcdefghij',None)
然后
params.update(kwargs)
或类似操作。。。这一切都取决于你想做什么。据我所知,还没有,但你可以写一个装饰师来处理。是的,可能有很多方法可以做到这一点,但都会带来某种“劣势”(至少如果你不在IDE级别上做),例如,这会严重限制内省。我的问题来自这个问题。我不得不写这么多次。那么更简单的方法是什么呢?如果一个函数有那么多默认值和参数,这意味着应该将这些参数封装在一个对象本身中(一个
dict
似乎是合理的),并将其传递给函数。然后,作为您的健全性检查/验证的一部分,您可以默认那里没有提供的任何内容…您甚至可以接受
**kwargs
,并在内部执行
params=dict.fromkeys('abcdefghij',None)
然后
params.update(kwargs)
或类似操作。。。这一切都取决于你想做什么。我知道这有一些属性,但我认为必须重新编译函数才能让它工作。这很聪明。我知道这有一些属性,但我认为必须重新编译函数才能使其工作。那很聪明。