简体   繁体   English

双星号 (**) 运算符给出不同的结果

[英]Double asterisk (**) operator gives different results

I have the following code我有以下代码

from werkzeug.datastructures import ImmutableMultiDict
f = ImmutableMultiDict([('name', ''), ('desc', 'example'), ('species', '1747')])
def print_data(name, desc, species):
    print(desc)

print_data(**f)

On python 3.6.1 the result is在 python 3.6.1 上,结果是

['example']

And on python 3.7.2 the result is在 python 3.7.2 上,结果是

'example'

I couldn't find any information about this change.我找不到有关此更改的任何信息。 And is seems strange that it is the result of the python version (BUT everything else is the same (werkzeug v1.0.1))它是 python 版本的结果似乎很奇怪(但其他一切都一样(werkzeug v1.0.1))

Am I right and this is a python thing?我是对的,这是一个蟒蛇吗? Can anyone point me to the release notes (Yes, I searched and didn't find...)谁能指出我的发行说明(是的,我搜索过但没有找到......)

Yes, CPython has changed behaviour in 3.7.1.是的,CPython 在 3.7.1 中改变了行为。 Paraphrasing this bug report on Werkzeug, which also contains the solution:在 Werkzeug 上解释此错误报告,其中还包含解决方案:

CPython [..] changed [..] the implementation of **kwargs to unpack using data.items() instead of dict.items(data) . CPython [..] 改变了 [..] **kwargs的实现以使用data.items()而不是dict.items(data)解包 MultiDict.items() always returned the first value, so CPython [was] previously [..] using dict.items(data) [..] MultiDict.items()总是返回第一个值,所以 CPython [是] 以前 [..] 使用dict.items(data) [..]

 m = MultiDict({"x": [1, 2]}) m.items() [("x", 1)] dict.items(m) [("x", [1, 2])]

There's not really anything Werkzeug can do about this, we're not going to change how MultiDict.items() works. Werkzeug 对此无能为力,我们不会改变MultiDict.items()工作方式。

I'm not even sure if **MultiDict was ever intended to work.我什至不确定**MultiDict是否曾经打算工作。 Use data.as_dict(flat=False) to get a dict to unpack.使用data.as_dict(flat=False)获取要解压的dict

 m.as_dict(flat=False) {"x": [1, 2]} fn(**m.as_dict(flat=False)) {"x": [1, 2]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM