Hacker News new | ask | show | jobs
by IshKebab 1385 days ago
Nah that's not a good enough example to motivate adding += to Python. You can just do `a.extend(b)`!
1 comments

It wasn't meant to be a convincing argument.

It was meant to show your comment at https://news.ycombinator.com/item?id=32741165 wasn't a relevant parallel, because it ignored how "a+=1" and "a=a+1" are different.

For a more convincing use, consider NumPy arrays, where a+=1 re-use the same (potentially very large) array, while a=a+1 creates a new array.

  import numpy
  a = b = numpy.array([[1,2], [3, 9]])
  a += 1  # modify in-place
  a = a + 1 # create a new array

  >>> a
  array([[ 3,  4],
         [ 5, 11]])

  >>> b
  array([[ 2,  3],
         [ 4, 10]])
In-place modification can improve performance over using intermediate/temporary arrays.