如何使用可选参数创建Python函数?

时间:2021-12-22 09:16:44

I have a Python function which takes several arguments. Some of these arguments could be omitted in some scenarios.

我有一个Python函数,它需要几个参数。在某些情况下,可以省略其中一些参数。

def some_function (self, a, b, c, d = None, e = None, f = None, g = None, h = None):
    #code

The arguments d through h are strings which each have different meanings. It is important that I can choose which optional parameters to pass in any combination. For example, (a, b, C, d, e), or (a, b, C, g, h), or (a, b, C, d, e, f, or all of them (these are my choices).

参数d到h是各自具有不同含义的字符串。重要的是我可以选择以任意组合传递哪些可选参数。例如,(a,b,C,d,e),或(a,b,C,g,h),或(a,b,C,d,e,f或它们全部(这些是我的)选择)。

It would be great if I could overload the function - but I read that Python does not support overloading. I tried to insert some of the required int arguments in the list - and got an argument mismatch error.

如果我可以重载函数会很棒 - 但我读到Python不支持重载。我试图在列表中插入一些必需的int参数 - 并得到一个参数不匹配错误。

Right now I am sending empty strings in place of the first few missing arguments as placeholders. I would like to be able to call a function just using actual values.

现在我发送空字符串代替前几个缺少的参数作为占位符。我希望能够使用实际值调用函数。

Is there any way to do this? Could I pass a list instead of the argument list?

有没有办法做到这一点?我可以传递一个列表而不是参数列表吗?

Right now the prototype using ctypes looks something like:

现在使用ctypes的原型看起来像:

_fdll.some_function.argtypes = [c_void_p, c_char_p, c_int, c_char_p, c_char_p, c_char_p, c_char_p, c_char_p]

2 个解决方案

#1


81  

Try calling it like: obj.some_function( '1', 2, '3', g="foo", h="bar" ). After the required positional arguments, you can specify specific optional arguments by name.

尝试调用它:obj.some_function('1',2,'3',g =“foo”,h =“bar”)。在所需的位置参数之后,您可以按名称指定特定的可选参数。

#2


104  

Just use the *args parameter, which allows you to pass as many arguments as you want after your a,b,c. You would have to add some logic to map args->c,d,e,f but its a "way" of overloading.

只需使用* args参数,它允许您在a,b,c之后传递任意数量的参数。你必须添加一些逻辑来映射args-> c,d,e,f但它是一种重载的“方式”。

def myfunc(a,b, *args, **kwargs):
   for ar in args:
      print ar
myfunc(a,b,c,d,e,f)

And it will print c,d,e,f

它将打印c,d,e,f


Similarly you could use the kwargs argument and then you could name your parameters.

类似地,您可以使用kwargs参数,然后您可以命名您的参数。

def myfunc(a,b, *args, **kwargs):
      c = kwargs.get('c', None)
      d = kwargs.get('d', None)
      #etc
myfunc(a,b, c='nick', d='dog', ...)

And then kwargs would have a dictionary of all the parameters that are key valued after a,b

然后kwargs会有一个字典,其中包含a,b之后键值的所有参数

#1


81  

Try calling it like: obj.some_function( '1', 2, '3', g="foo", h="bar" ). After the required positional arguments, you can specify specific optional arguments by name.

尝试调用它:obj.some_function('1',2,'3',g =“foo”,h =“bar”)。在所需的位置参数之后,您可以按名称指定特定的可选参数。

#2


104  

Just use the *args parameter, which allows you to pass as many arguments as you want after your a,b,c. You would have to add some logic to map args->c,d,e,f but its a "way" of overloading.

只需使用* args参数,它允许您在a,b,c之后传递任意数量的参数。你必须添加一些逻辑来映射args-> c,d,e,f但它是一种重载的“方式”。

def myfunc(a,b, *args, **kwargs):
   for ar in args:
      print ar
myfunc(a,b,c,d,e,f)

And it will print c,d,e,f

它将打印c,d,e,f


Similarly you could use the kwargs argument and then you could name your parameters.

类似地,您可以使用kwargs参数,然后您可以命名您的参数。

def myfunc(a,b, *args, **kwargs):
      c = kwargs.get('c', None)
      d = kwargs.get('d', None)
      #etc
myfunc(a,b, c='nick', d='dog', ...)

And then kwargs would have a dictionary of all the parameters that are key valued after a,b

然后kwargs会有一个字典,其中包含a,b之后键值的所有参数