字典文字的评估顺序 [英] Evaluation order of dictionary literals

查看:63
本文介绍了字典文字的评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def key(): print 'key'
def val(): print 'val'

{key() : val()}

打印val, key,即首先评估该值.这是行为

prints val, key, i.e. the value is evaluated first. Is this behaviour

  • consistent across python versions and implementations?
  • documented anywhere? (http://docs.python.org/2/reference/expressions.html#dictionary-displays says nothing)

推荐答案

参考手册的此部分记录了订单,但声称与您所看到的不同:

This section of the reference manual documents the order, but claims it is different than what you are seeing: http://docs.python.org/2/reference/expressions.html#evaluation-order

在以下几行中,将按后缀的算术顺序对表达式求值:

In the following lines, expressions will be evaluated in the arithmetic order of their suffixes:

expr1, expr2, expr3, expr4
(expr1, expr2, expr3, expr4)
{expr1: expr2, expr3: expr4}
expr1 + expr2 * (expr3 - expr4)
expr1(expr2, expr3, *expr4, **expr5)
expr3, expr4 = expr1, expr2

并且有一个针对该实现的公开错误:字典显示的评估顺序与参考手册不同.

And there is an open bug against the implementation about it: Evaluation order of dictionary display is different from reference manual.

这篇关于字典文字的评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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