3

Just saw (and enjoyed) the video of Brandon Rhodes talking on PyCon 2015 about bytearrays.

He said that .extend method is slow, but += operation is implemented differently and is much more efficient. Yes, indeed:

>>> timeit.timeit(setup='ba=bytearray()', stmt='ba.extend(b"xyz123")', number=1000000)
0.19515220914036036
>>> timeit.timeit(setup='ba=bytearray()', stmt='ba += b"xyz123"', number=1000000)
0.09053478296846151

What is the reason of having two ways of extending a bytearray? Are they performing exactly the same task? If not, what is the difference? Which one should be used when?

VPfB
  • 14,927
  • 6
  • 41
  • 75
  • Among other things, you can't chain `+=` like function calls, since you can't assign to a function call. And there's a variable locality requirement for `+=` but not for `.extend()`. But `+=` is slightly faster. – Roope Oct 12 '16 at 17:20
  • @Roope What is a variable locality requirement? – VPfB Oct 12 '16 at 17:23
  • `+=` cannot be used for a [nonlocal variable](https://www.dotnetperls.com/nonlocal-python). – Roope Oct 12 '16 at 17:26
  • @Roope I see. Thank you for explaining. We have a method and an operator and they are used differently. But the main question is why `+=` and `.extend` do not share the same internal function to do the actual work of extending a bytearray. – VPfB Oct 12 '16 at 17:32

1 Answers1

0

What is the reason of having two ways of extending a bytearray?

  • An operator is not chainable like function calls, whereas a method is.
  • The += operator cannot be used with nonlocal variables.
  • The += is slightly faster
  • .extend() may/might/could sometimes possibly be more readable

Are they performing exactly the same task?

Depends on how you look at it. The implementation is not the same, but the result usually is. For a bunch of examples and explanations, maybe try the SO search, and for example this question: Concatenating two lists - difference between '+=' and extend()

Which one should be used when?

If you care about the small performance difference, use the operator when you can. Other than that, just use whichever you like to look at, of course given the restrictions I mentioned above.

But the main question is why += and .extend do not share the same internal function to do the actual work of extending a bytearray.

Because one is faster but has limitations, so we need the other for the cases where we do have the limitations.


Bonus:

The increment operator might cause some funny business with tuples:

if you put a list in a tuple and use the += operator to extend the list, the increment succeeds and you get a TypeError

Source: https://emptysqua.re/blog/python-increment-is-weird-part-ii/

Community
  • 1
  • 1
Roope
  • 4,469
  • 2
  • 27
  • 51