64

This code:

a = [1, 2, 3]
print(*a, a.pop(0))

Python 3.8 prints 2 3 1 (does the pop before unpacking).
Python 3.9 prints 1 2 3 1 (does the pop after unpacking).

What caused the change? I didn't find it in the changelog.

Edit: Not just in function calls but also for example in a list display:

a = [1, 2, 3]
b = [*a, a.pop(0)]
print(b)

Prints [2, 3, 1] vs [1, 2, 3, 1]. And Expression lists says "The expressions are evaluated from left to right" (that's the link to Python 3.8 documentation), so I'd expect the unpacking expression to happen first.

12
  • 9
    Was this ever defined or guaranteed behavior in the first place…?
    – deceze
    Dec 18, 2021 at 15:15
  • 4
    @deceze A language should give consistent results if there are no changes made to it. Dec 18, 2021 at 15:19
  • 16
    @ElderYeager: not necessarily. if this was undefined, you should have never used it in the first place
    – blue_note
    Dec 18, 2021 at 15:20
  • 2
    Somewhat related Is Python's order of evaluation of function arguments and operands deterministic (+ where is it documented)? - Note the second comment about bugs by @wim
    – buran
    Dec 18, 2021 at 15:29
  • 2
    @CodyGray I'd say it depends on what one means with "undefined behavior", but maybe stuff like the order of set elements, much of the stuff that the documentation calls "CPython implementation detail", or stuff like what searching "undefined" finds. Dec 19, 2021 at 11:36

1 Answer 1

53

I suspect this may have been an accident, though I prefer the new behavior.

The new behavior is a consequence of a change to how the bytecode for * arguments works. The change is in the changelog under Python 3.9.0 alpha 3:

bpo-39320: Replace four complex bytecodes for building sequences with three simpler ones.

The following four bytecodes have been removed:

  • BUILD_LIST_UNPACK
  • BUILD_TUPLE_UNPACK
  • BUILD_SET_UNPACK
  • BUILD_TUPLE_UNPACK_WITH_CALL

The following three bytecodes have been added:

  • LIST_TO_TUPLE
  • LIST_EXTEND
  • SET_UPDATE

On Python 3.8, the bytecode for f(*a, a.pop()) looks like this:

  1           0 LOAD_NAME                0 (f)
              2 LOAD_NAME                1 (a)
              4 LOAD_NAME                1 (a)
              6 LOAD_METHOD              2 (pop)
              8 CALL_METHOD              0
             10 BUILD_TUPLE              1
             12 BUILD_TUPLE_UNPACK_WITH_CALL     2
             14 CALL_FUNCTION_EX         0
             16 RETURN_VALUE

while on 3.9, it looks like this:

  1           0 LOAD_NAME                0 (f)
              2 BUILD_LIST               0
              4 LOAD_NAME                1 (a)
              6 LIST_EXTEND              1
              8 LOAD_NAME                1 (a)
             10 LOAD_METHOD              2 (pop)
             12 CALL_METHOD              0
             14 LIST_APPEND              1
             16 LIST_TO_TUPLE
             18 CALL_FUNCTION_EX         0
             20 RETURN_VALUE

In the old bytecode, the code pushes a and (a.pop(),) onto the stack, then unpacks those two iterables into a tuple. In the new bytecode, the code pushes a list onto the stack, then does l.extend(a) and l.append(a.pop()), then calls tuple(l).

This change has the effect of shifting the unpacking of a to before the pop call, but this doesn't seem to have been deliberate. Looking at bpo-39320, the intent was to simplify the bytecode instructions, not to change the behavior, and the bpo thread has no discussion of behavior changes.

6
  • The answers to the linked Q state the order is left to right, and give also the example expr1(expr2, expr3, *expr4, **expr5). So that doesn't seem to be correct for versions up to Python 3.8, right? What happens in 3.9 if the two are in opposite order, print(a.pop(0), *a)?
    – ilkkachu
    Dec 19, 2021 at 0:08
  • 1
    @ilkkachu: The expressions are evaluated left to right, but *expr4 is not an expression. expr4 is. The * is part of the function call syntax, and the evaluation order documentation doesn't make any promises about when the unpacking happens. Dec 19, 2021 at 0:41
  • 2
    As for print(a.pop(0), *a), the pop happens before the unpacking, regardless of version. Dec 19, 2021 at 0:42
  • @user2357112supportsMonica "*a isn't an expression." - I'm not sure that argument holds water. If I enter a bare *a in Python 3.9, I get SyntaxError: can't use starred expression here, so clearly Python thinks it's some sort of expression. The fact it's not tolerated in every context doesn't change that.
    – marcelm
    Dec 19, 2021 at 10:31
  • 3
    @marcelm The term "starred expression" could just mean an expression which is starred; it doesn't imply that the result of starring an expression is another expression. Compare e.g. the term "truncated word", which doesn't necessarily imply that something like flowe (a truncation of the word flower) is itself a word.
    – kaya3
    Dec 19, 2021 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.