Python脚本的文字是按照书写的顺序进行评估吗? [英] Will a Python dict literal be evaluated in the order it is written?

查看:134
本文介绍了Python脚本的文字是按照书写的顺序进行评估吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在Python中有这样的情况:

  _avg = {'total':0.0, 'count':0}#HACK:这里存储的副作用

def procedure(n):
_avg ['count'] + = 1
_avg ['total' ] + = n
return n

def get_average():
return _avg ['total'] / _avg ['count']

my_dict = {
'key0':procedure(0),
'key2':procedure(2),
'key1':get_average()
}
assert my_dict ['key1'] == 1.0)

我知道 my_dict.keys()是未定义的,但我想知道的是,通过这样的字面意思,初始化是否保证以特定的顺序发生。 my_dict ['key1'] 的值始终为 1.0 被断言吗?

解决方案

字典评估顺序应该与写的相同,但是有一个未知错误其中之前被评估。 (该错误最终在Python 3.5中修复)。



参考文档


Python从左到右评估表达式。


和错误报告:


运行以下代码显示2 1 4 3,但在参考手册中
http://docs.python.org/reference/expressions.html#expression-lists
评估顺序描述为 {expr1:expr2,expr3:expr4}

  def f(i) 
print i
return i

{f(1):f(2),f(3):f(4)}


Guido说:

$ b $我从前面坚持我的意见:代码应该是固定的。它不像我的任务。


这个错误在Python 3.5中是修复的,所以在Python 3.4及更早版本中仍然在键之前进行评估:

 >>>导入sys 
>>>> sys.version_info
sys.version_info(major = 3,minor = 4,micro = 2,releaselevel ='final',serial = 0)
>>> def f(i):
... print(i)
... return i
...
>>> {f(1):f(2),f(3):f(4)}
2
1
4
3
{1: 3:4}

由于您的代码不需要首先评估密钥,因此您的代码是保证正常工作;即使在每个对应的值之后对键进行评估,键值对仍然依次进行评估。


Let's say that I've got a situation like this in Python:

_avg = {'total':0.0, 'count':0}    # HACK: side-effects stored here

def procedure(n):
  _avg['count'] += 1
  _avg['total'] += n
  return n

def get_average():
  return _avg['total'] / _avg['count']

my_dict = {
  'key0': procedure(0),
  'key2': procedure(2),
  'key1': get_average()
}
assert(my_dict['key1'] == 1.0)

I know that the order of my_dict.keys() is undefined, but what I'm wondering is whether the initialization via a literal like this is guaranteed to happen in a particular order. Will the value of my_dict['key1'] always be 1.0 as asserted?

解决方案

Dictionary evaluation order should be the same as written, but there is a outstanding bug where values are evaluated before the keys. (The bug was finally fixed in Python 3.5).

Quoting from the reference documentation:

Python evaluates expressions from left to right.

and from the bug report:

Running the following code shows "2 1 4 3", but in reference manual http://docs.python.org/reference/expressions.html#expression-lists the evaluation order described as {expr1: expr2, expr3: expr4}

def f(i):
    print i
    return i

{f(1):f(2), f(3):f(4)}

and Guido stated:

I am sticking with my opinion from before: the code should be fixed. It doesn't look like assignment to me.

This bug is fixed in Python 3.5, so on Python 3.4 and earlier the values are still evaluated before the keys:

>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=4, micro=2, releaselevel='final', serial=0)
>>> def f(i):
...     print(i)
...     return i
... 
>>> {f(1):f(2), f(3):f(4)}
2
1
4
3
{1: 2, 3: 4}

Since your code doesn't require the keys to be evaluated first, your code is guaranteed to work correctly; key-value pairs are still evaluated in order even if the keys are evaluated after each corresponding value.

这篇关于Python脚本的文字是按照书写的顺序进行评估吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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