````在`dict(d1,** d2)'中是什么意思? [英] What does `**` mean in the expression `dict(d1, **d2)`?

查看:1136
本文介绍了````在`dict(d1,** d2)'中是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下python表达式感兴趣:

I am intrigued by the following python expression:

d3 = dict(d1, **d2)

任务是将2个词典合并成第三个,并且上述表达式完成任务。我对**操作符感兴趣,它对表达式究竟是做什么的。我以为**是电力运营商,没有看到它在上述的上下文中使用。

The task is to merge 2 dictionaries into a third one, and the above expression accomplishes the task just fine. I am interested in the ** operator and what exactly is it doing to the expression. I thought that ** was the power operator and haven't seen it used in the context above yet.

完整的代码片段是这样的:

The full snippet of code is this:

>>> d1 = {'a': 1, 'b': 2}
>>> d2 = {'c': 3, 'd': 4}
>>> d3 = dict(d1, **d2)
>>> print d3
{'a': 1, 'c': 3, 'b': 2, 'd': 4}


推荐答案

** 在参数列表中有一个特殊的含义,如本教程的第4.7节。用 ** kwargs 传递的字典(或类似字典)的对象被扩展为可调用的关键字参数,就像 * args 扩展为单独的位置参数。

** in argument lists has a special meaning, as covered in section 4.7 of the tutorial. The dictionary (or dictionary-like) object passed with **kwargs is expanded into keyword arguments to the callable, much like *args is expanded into separate positional arguments.

这篇关于````在`dict(d1,** d2)'中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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