是否按顺序评估Python字典文字?

参见英文答案 > Will a Python dict literal be evaluated in the order it is written?                                    3个
当我跑这个

import itertools

bar = itertools.count(1).next

print {'a': bar(),'b': bar(),'c': bar()}

cpython上,我得到{‘a’:0,’b’:1,’c’:2}.这在实施中是否一致和/或在任何地方都有记录? a,b和c是否会被无序分配值?

解决方法

Python Docs(强调我的):

If a comma-separated sequence of key/datum pairs is given,they are evaluated from left to right to define the entries of the dictionary: each key object is used as a key into the dictionary to store the corresponding datum….

因此,将始终按顺序评估对.但不要混淆元素的顺序.

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...