classification
Title: Inline assignment uses the newly created object
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, chupym, r.david.murray
Priority: normal Keywords:

Created on 2013-01-24 09:30 by chupym, last changed 2013-02-01 14:35 by r.david.murray. This issue is now closed.

Messages (4)
msg180518 - (view) Author: Gabriel Nistor (chupym) Date: 2013-01-24 09:30
I am using Lubuntu x64 version and python 3.2.3
I have a tree search method:

node = self
while xpaths:
    xpath = xpaths.popleft()
    for path, child in node.childrens.items():
        if path == xpath:
            node = child
            break
    else:
        node = node.childrens[xpath] = Node(xpath)
return node

This fails because the node is created and then the node.childrens[xpath] assignation is done on the new created node rather then the old node, I needed to do something like:
        child = node.childrens[xpath] = Node(xpath)
        node = child
to have the proper behavior.
msg180522 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-01-24 13:46
I agree that this is somewhat surprising, but it is working as intended.

   a = b = c

is equivalent to

   a = c
   b = c

except that the RHS is evaluated only once, which can be important.

You were either expecting it to be equivalent to

   b = c
   a = c

or expecting all of the LHS expressions to be evaluated before any assignments were done.

It could have been implemented any of these ways (though the last one is much more complex to implement). Python chose the first way.

There is a specific warning about this in the documentation, at the end of the section on assignment statements:

http://docs.python.org/3/reference/simple_stmts.html#assignment-statements
msg181071 - (view) Author: Gabriel Nistor (chupym) Date: 2013-02-01 08:02
Thanks for the fast reply. The explanation seems valid, but the behavior is not consistent with other high level languages, and also with plain reasoning. I have a big java background experience and I am now with python for almost 3 years doing really hard core stuff, and I am still surprised by some solutions that you have adopted (like GIL for instance) and really happy about others, anyway thank you for a great language.
msg181080 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-02-01 14:35
Well, it is consistent with plain reasoning if you remember that (a) python is a dynamic language and (b) python assignments do not return values (this is a core principle in the language design), which means that (c) the chained assignment form is a shorthand.  Of course, you do then have to look up what it is a shorthand *for*, but if you approached another language's chained assignment with a Python mindset, you'd also have to look it up to find out what rules applied to that language.

It is hard not to make assumptions based on other languages you've learned.  Interestingly, when I did a quick google for what other languages do for chained assignment I didn't come up with much.  Looks like most don't support it, or if they do they do so as a side effect of an assignment returning a value.

So, yes, Python is a unique language.  There are usually good underlying reasons for the various design decision made (not always, but very very often).
History
Date User Action Args
2013-02-01 14:35:03r.david.murraysetmessages: + msg181080
2013-02-01 08:02:06chupymsetmessages: + msg181071
2013-01-24 13:46:42r.david.murraysetstatus: open -> closed

type: behavior
title: Inline assignation uses the newly created object -> Inline assignment uses the newly created object
nosy: + r.david.murray

messages: + msg180522
resolution: not a bug
stage: resolved
2013-01-24 10:46:51pitrousetnosy: + benjamin.peterson
2013-01-24 09:30:55chupymcreate