为什么dict(k = 4,z = 2).update(dict(l = 1))在Python中返回None? [英] Why does dict(k=4, z=2).update(dict(l=1)) return None in Python?

查看:278
本文介绍了为什么dict(k = 4,z = 2).update(dict(l = 1))在Python中返回None?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 dict(k = 4,z = 2).update(dict(l = 1)) return None ?似乎应该返回 dict(k = 4,z = 2,l = 1)?我使用Python 2.7应该这么重要。

Why does dict(k=4, z=2).update(dict(l=1)) return None? It seems as if it should return dict(k=4, z=2, l=1)? I'm using Python 2.7 should that matter.

推荐答案

.update()方法更改字典到位,并返回。字典本身已更改,不需要更改字典。

The .update() method alters the dictionary in place and returns None. The dictionary itself is altered, no altered dictionary needs to be returned.

首先分配字典:

a_dict = dict(k=4, z=2)
a_dict.update(dict(l=1))
print a_dict

这是清楚的记录,请参阅 dict.update()方法文档

This is clearly documented, see the dict.update() method documentation:


使用其他键/值对更新字典,覆盖现有键。返回

这篇关于为什么dict(k = 4,z = 2).update(dict(l = 1))在Python中返回None?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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