Dictionary evaluation order
I was just looking at the bug [#448679] Left to right It mentions that code like that {f1():f2(), f3():f4()} Will call these functions in the order f2, f1, f4, f3. What should we do about it? Tim mentions that "When [Tim] asked Guido about that some years ago, he agreed it was a bug.". Is it too late to fix it, or is it still a desirable fix? The fix should be as easy as that: diff -u -r2.264 compile.c --- Python/compile.c 3 Oct 2002 09:50:47 -0000 2.264 +++ Python/compile.c 26 Nov 2002 04:02:27 -0000 @@ -1527,9 +1527,9 @@ It wants the stack to look like (value) (dict) (key) */ com_addbyte(c, DUP_TOP); com_push(c, 1); - com_node(c, CHILD(n, i+2)); /* value */ - com_addbyte(c, ROT_TWO); com_node(c, CHILD(n, i)); /* key */ + com_node(c, CHILD(n, i+2)); /* value */ + com_addbyte(c, ROT_THREE); com_addbyte(c, STORE_SUBSCR); com_pop(c, 3); } (compiler module should be fixed as well, as it mimicks that behavior) So I belive it just a matter of deciding what should be done. -- Gustavo Niemeyer [ 2AAC 7928 0FBF 0299 5EB5 60E2 2253 B29A 6664 3A0C ]
I think the language shouldn't specify what the order of evaluation here. The current implementation seems just as valid as the proposed change. No one should write code that depends on this order. Jeremy
I was just looking at the bug
[#448679] Left to right
It mentions that code like that
{f1():f2(), f3():f4()}
Will call these functions in the order f2, f1, f4, f3. What should we do about it? Tim mentions that "When [Tim] asked Guido about that some years ago, he agreed it was a bug.". Is it too late to fix it, or is it still a desirable fix?
Hm, there are other situations where it's not so easy to get strict L2R evaluation, e.g. a = {} a[f1()] = f2() It's rather natural to evaluate the RHS first in assignments. Since the dict display is a thinly veiled assignment, I'm not sure it's worth fixing this in the language definition. What does Jython do? --Guido van Rossum (home page: http://www.python.org/~guido/)
[Gustavo Niemeyer]
I was just looking at the bug
[#448679] Left to right
It mentions that code like that
{f1():f2(), f3():f4()}
Will call these functions in the order f2, f1, f4, f3. What should we do about it? Tim mentions that "When [Tim] asked Guido about that some years ago, he agreed it was a bug.". Is it too late to fix it, or is it still a desirable fix?
[Guido van Rossum]
... What does Jython do?
Jython happens to evaluate in order of f1, f2, f3, f4. An accident, I'm sure, of the way the dictionary constructor is called with a sequence of (key, value, key, value, ...). regards, finn
It mentions that code like that
{f1():f2(), f3():f4()}
Will call these functions in the order f2, f1, f4, f3. What should we do about it? Tim mentions that "When [Tim] asked Guido about that some years ago, he agreed it was a bug.". Is it too late to fix it, or is it still a desirable fix?
[Guido van Rossum]
... What does Jython do?
Jython happens to evaluate in order of f1, f2, f3, f4. An accident, I'm sure, of the way the dictionary constructor is called with a sequence of (key, value, key, value, ...).
In that case, I see no reason to block the fix for this particular case. --Guido van Rossum (home page: http://www.python.org/~guido/)
Note that the primary point of the referenced bug report http://www.python.org/sf/448679 Left to right is that the Ref Man doesn't really address Python's evaluation order. Guido has said he intended left-to-right, in which case the dict example would be an endcase glitch. I don't believe the language definition currently requires or forbids any specific eval order here, though.
Note that the primary point of the referenced bug report
http://www.python.org/sf/448679 Left to right
is that the Ref Man doesn't really address Python's evaluation order.
Yes, I'm aware about that. I'd just like to fix that, or to document the exception, together with the evaluation order documentation.
Guido has said he intended left-to-right, in which case the dict example would be an endcase glitch. I don't believe the language definition currently requires or forbids any specific eval order here, though.
Ok. I can take the following conclusions then. Please, correct me if I'm wrong. - everyone seems to agree that the current behavior is not set in stone; - no one should expect the current behavior, as it is not documented, and not good to expect such language behavior anyway; - having the L2R order where possible would be good and was originaly intended; - Jython already uses L2R in that case as well; Based on that, I'll write a suggested solution including documentation, and post for review. Thanks everyone. -- Gustavo Niemeyer [ 2AAC 7928 0FBF 0299 5EB5 60E2 2253 B29A 6664 3A0C ]
participants (5)
-
Finn Bock
-
Guido van Rossum
-
Gustavo Niemeyer
-
Jeremy Hylton
-
Tim Peters