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

我有一个带几个参数的Python函数。在某些情况下,可以忽略其中一些参数。

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

这些参数d通过h是字符串,每个都有不同的含义。我可以选择要以任何组合形式传递的可选参数,这一点很重要。例如,(a, b, C, d, e)(a, b, C, g, h),,或(a, b, C, d, e, f,或所有它们(这些是我的选择)。

如果我可以重载该函数,那就太好了-但我读到Python不支持重载。我试图在列表中插入一些必需的int参数-并收到参数不匹配错误。

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

有什么办法吗?我可以传递一个列表而不是参数列表吗?

现在,使用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]

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

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).

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.

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:

_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]

回答 0

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

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.


回答 1

只需使用*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)

它会打印出 c,d,e,f


同样,您可以使用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', ...)

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

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.

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

And it will print values of c,d,e,f


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

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


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。