1

I noticed a strange behavior while using the splat operator.

I ran the following code on different computers:

def func(m):
    m(1)
    return []
    
messages = []
print(
    [*messages, *func(messages.append)]
)

Test 1

  • PC: MacBook Air M1
  • Python version: 3.9.7
  • Output: []

Test 2

  • PC: MacBook Pro Intel
  • Python version: 3.8.9
  • Output: [1]

Test 3

  • Google Colab
  • Python version: 3.7.12
  • Output: [1]

Test 4

My first impression was that the splat was applied to messages before m(1) in Test 1, but after in the other tests. Since this doesn't seem to be associated to the Python version, I guess there's something different with the M1 chip...

I couldn't find a clear explanation of what should happen here: https://docs.python.org/3/reference/expressions.html#evaluation-order

David Sand
  • 11
  • 2
  • Which 3.10 version exactly? I was under the impression that 3.9 and newer would result in `[]`. – Kelly Bundy Feb 01 '22 at 19:36
  • Would be good to do `print( [*messages, *func(messages.append)], sys.version )` to be sure you're running the version you think you're running. – Kelly Bundy Feb 01 '22 at 19:46
  • 2
    You are correct Kelly, version 3.10 also results in [], so the question was wrong. The answer is the Python version as you suspected. Thank you for the response :) – David Sand Feb 04 '22 at 21:04

0 Answers0