如何在函数签名中使用* args和** kwargs返回默认值 [英] How to return default values with *args, and **kwargs in function signature

查看:70
本文介绍了如何在函数签名中使用* args和** kwargs返回默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Python 3(Python 3.7.0)中使用 args kwargs 来解决问题,但是我的理解遇到了一些问题

I'm trying to wrap my head around using args and kwargs in Python 3 (Python 3.7.0) but I'm running into some issues with my understanding.

这是我拥有的一个简单函数:

Here is a simple function I have:

def add_args(y=10, *args, **kwargs):
    return y, args, kwargs

并对其进行测试以查看返回的结果:

And test it to see what is returned:

    print(add_args(1, 5, 10, 20, 50))
    print(add_args())
    >>
    (1, (5, 10, 20, 50), {}) # What happened to my default y=10?
    (10, (), {})

我不明白的是,在第一个打印语句中 y = 10 发生了什么?我可以看到它被 args 中的 1 覆盖,但是我不确定为什么.

What I don't understand is, what happened to y=10 in the first print statement? I can see it is being overridden by the 1 in args but I'm unsure why.

如何重写此函数,以便不覆盖默认值,还是我缺少一些有关如何将参数从函数签名传递到return语句的内容?

How can I rewrite this function so the default value is not overridden, or am I missing something with how the parameters are passed from the function signature to the return statement?

我尝试在此处此处,但没有找到我想要的答案.正如我认为的那样,将默认值放在 args kwargs 之前可以防止覆盖.

I tried looking here and here but did not find the answers I was looking for. As I thought putting the default values before the args and kwargs would prevent the overwriting.

推荐答案

* args 仅捕获其他未定义的位置参数; y = 10 并不意味着 y 不能用作位置参数.因此,为 y 分配了第一个位置参数.

*args only captures any positional arguments not otherwise defined; y=10 does not mean y can't be used as a positional argument. So y is assigned the first positional argument.

通过将 y 用作

You can prevent y being used as a positional argument by making it a keyword-only argument. You do this by placing the argument after the *args var-positional catch-all parameter, or if you don't have a *name parameter, after a * single asterisk:

def add_args(*args, y=10, **kwargs):
    return y, args, kwargs

def keyword_only_args(*, y=10, **kwargs):
    return y, kwargs

现在 y 将不再捕获位置参数:

Now y won't capture positional arguments any more:

>>> def add_args(*args, y=10, **kwargs):
...     return y, args, kwargs
...
>>> add_args(1, 5, 10, 20, 50)
(10, (1, 5, 10, 20, 50), {})          # y is still 10
>>> add_args(1, 5, 10, 20, 50, y=42)  # setting y explicitly 
(42, (1, 5, 10, 20, 50), {})

您不必全部使用 ** kwargs 关键字:

You don't have to have a **kwargs keyword catch-all either:

def add_args(*args, y=10):
    return y, args

但如果存在,则需要在最后列出.

but if it is present, it needs to be listed last.

仅关键字参数不必具有默认值,可以省略 = 10 ,但是随后该参数成为必需参数,并且只能在调用中使用指定y = value :

Keyword-only arguments do not have to have a default value, the =10 can be omitted, but then the parameter becomes mandatory, and can only be specified in a call by using y=value:

>>> def add_args(*args, y):  # mandatory keyword-only argument
...     return y, args
...
>>> add_args(1, 5, 10, 20, 50)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: add_args() missing 1 required keyword-only argument: 'y'
>>> add_args(1, 5, 10, 20, 50, y=42)
(42, (1, 5, 10, 20, 50))

这篇关于如何在函数签名中使用* args和** kwargs返回默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆