0
Python 3.9.6 (default, Aug 18 2021, 19:38:01) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = []
>>> a += b'1'
>>> a
[49]
>>> [] + b'1'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate list (not "bytes") to list

The first variant gets a list of ints, the second fails because bytes can not be concatenated to a list, which makes sense to me.

What causes the difference in behaviour? I am surprised just storing a result in a variable seems to cause this. Does the += operator not make use of the + operator?

lucidbrot
  • 4,585
  • 3
  • 33
  • 60
  • 1
    It does not appear to be because of the variable, rather, it's [due to differences between += and + for lists on other sequences](https://stackoverflow.com/questions/6951792/a-b-not-the-same-as-a-a-b?noredirect=1&lq=1) – kcsquared Sep 05 '21 at 15:25

0 Answers0