The procedure for evaluating vocabulary literals

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

{key() : val()}

prints val, key, that is, the value is first evaluated. This behavior

+5
source share
2 answers

This section of the reference guide shows the order, but it claims to be different from what you see: http://docs.python.org/2/reference/expressions.html#evaluation-order

In the following lines, expressions will be calculated in 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

, : .

+10

, .

def p(x):
    print(x)
    return x

{p('k1'): p('v1'), p('k2'): p('v2'), p('k3'): p('v3')}

:

Python 2.7.3 (default, Aug  1 2012, 05:16:07) 
IronPython 2.7.3 (2.7.0.40) on Mono 4.0.30319.1 (32-bit)
[PyPy 2.0.0-beta1 with GCC 4.4.3] on linux2

v1
k1
v2
k2
v3
k3

Jython 2.5.1+ (Release_2_5_1, Oct 31 2011, 11:44:27) 
[OpenJDK Server VM (Sun Microsystems Inc.)] on java1.6.0_27
Jython 2.7b1 (default:ac42d59644e9, Feb 9 2013, 15:24:52) 
[OpenJDK Server VM (Sun Microsystems Inc.)] on java1.6.0_27

k1
v1
k2
v2
k3
v3
+5

All Articles