3

I was looking at a simple code that I did not understand. I think it is the same as the operation priority, but I did not know it properly.

stack = ["apple", 1]
heap = {}
heap[stack.pop()] = stack.pop()

In JavaScript, the result of this code is heap = {1:"apple"}.
But the result is heap = {"apple" : 1} in python.

I would like to know why Python has these results.

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
  • @wim Your answer in your suggested dupe does not include this specific example – jamylak Jan 29 '19 at 02:00
  • 1
    @jamylak Not exactly, but it's a special case of the last example `expr3, expr4 = expr1, expr2`. – wim Jan 29 '19 at 02:02
  • 2
    @wim: Not to mention the top answer quotes from the docs "Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side." That by itself answers the question; the `pop` on the right of the assignment is guaranteed to evaluate before the `pop` on the left, so the last value of the `list` becomes the dict's value, while the second to last becomes the dict's key. – ShadowRanger Jan 29 '19 at 02:06
  • What i want answers are these comments. However, I accidentally accepted the below answer. ;ㅅ; Over time, I found the answers I wanted in the following 2 links . Thanks to informed me. [link1](https://stackoverflow.com/questions/45247309/javascript-evaluation-order-when-assigning?fbclid=IwAR0GVivsevLOSrIdptadsNxeY8v_4yGC6vxan8hZuRS3ZsLMrEztfhdQRDw) [link2](https://stackoverflow.com/questions/46288616/is-pythons-order-of-evaluation-of-function-arguments-and-operands-deterministic?fbclid=IwAR3tn1QOheGIuWeb78m2NLqsBwPnFFkObAdl0mrdMiqX9paEiVRWW-lr_-M) – ReactScript Jan 30 '19 at 08:15

1 Answers1

-2

Your code taken step by step may help you understand what is going on

In [1]: stack = ["apple", 1]

In [2]: stack
Out[2]: ['apple', 1]

In [3]: pop1 = stack.pop()

In [4]: pop1
Out[4]: 1

In [5]: stack
Out[5]: ['apple']

In [6]: pop2 = stack.pop()

In [7]: pop2
Out[7]: 'apple'

In [8]: stack
Out[8]: []

In [9]: heap = {}

In [10]: heap
Out[10]: {}

In [11]: heap[pop2] = pop1

In [12]: heap
Out[12]: {'apple': 1}

As you can see the pop method first removes the last item of the stack list and returns it, then the second last.

moctarjallo
  • 1,479
  • 1
  • 16
  • 33
  • 2
    This doesn't actually explain why, *on a single line*, the left hand `pop` executes after the right hand `pop`. Obviously if you execute each one in separate statements, you'll get whatever result you like. – ShadowRanger Jan 29 '19 at 02:05
  • Every programming language has that feature: the right hand side always gets executed first !!! Also this is just to help him do further research, no more. – moctarjallo Jan 29 '19 at 02:08
  • 1
    Umm... The whole point of the OP's question is that it *doesn't* behave this way in JavaScript. So clearly not *every* programming language behaves this way. Trying to "guide further research" is fine, but it's not an answer to the question. – ShadowRanger Jan 29 '19 at 02:10
  • 2
    _"the right hand side always gets executed first"_ Nope. (In)Famously C++'s order was **undefined** (this has changed): https://stackoverflow.com/a/8295391 And while looking for that link, I learned that C# guarantees left first! https://blogs.msdn.microsoft.com/oldnewthing/20170718-00/?p=96635 – jscs Jan 29 '19 at 02:12
  • The order doesn't either change in javascript. What changes is the behaviour of list operations. In javascript ```pop``` returns the first element and goes from left to right while removing elements, in python it goes from right to left and that's what i tried to demonstrate to him here ;-) – moctarjallo Jan 29 '19 at 02:18
  • After all.. he's the only one suited to decide if this helped him or not i guess ;-) – moctarjallo Jan 29 '19 at 02:52
  • @mctrjalloh: "In javascript `pop` returns the first element and goes from left to right while removing elements..." Umm, no. [`Array.pop`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop) returns the last element, just like in Python. You're thinking of [`Array.shift`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift). – ShadowRanger Jan 29 '19 at 04:04