[Python-Dev] chained assignment weirdity

Nick Coghlan ncoghlan at gmail.com
Wed Nov 7 15:11:41 CET 2012


On Wed, Nov 7, 2012 at 10:13 PM, Ned Batchelder <ned at nedbatchelder.com> wrote:
> There are plenty of places where different Python implementations differ,
> and even careful observers had different ideas about how keys and values
> were ordered in dict displays ("I thought it was like a function call,"  vs,
> "I thought it was like an assignment"). We've gone out of our way to
> maintain backward compatibility with the implemented behavior before
> (ordering of dict keys, for example).  The simplest change to make here is
> to update the reference and keep the implementation.

"The implementation is right, the docs are wrong" sounds good to me,
as it's easy to justify the out of order evaluation in terms of the
equivalent item assignment statements:

    x = {a:b, c:d}

vs

    x = {}
    x[a] = b
    x[c] = d

That relationship is quite logical given that (ignoring namespace
details) dict construction from a display [1] pretty much does the
equivalent of:

    result = {}
    for key_expr, val_expr in display_entries:
        result[eval(key_expr)] = eval(val_expr)

This comment [2] from the dict comprehension implementation makes it
explicit that the behaviour of the equivalent Python item assignment
code was taken to be normative.

[1] http://hg.python.org/cpython/file/default/Python/compile.c#l3319
[2] http://hg.python.org/cpython/file/default/Python/compile.c#l3020

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list