surprising += for lists

  • Thread starter Ulrich Eckhardt
  • Start date
U

Ulrich Eckhardt

Hi everybody!

I was just smacked by some very surprising Python 2.7 behaviour. I was
assembling some 2D points into a list:

points = []
points += (3, 5)
points += (4, 6)

What I would have expected is to have [(3, 5), (4, 6)], instead I got [3,
5, 4, 6]. My interpretations thereof is that the tuple (x, y) is iterable,
so the elements are appended one after the other. Actually, I should have
used points.append(), but that's a different issue.

Now, what really struck me was the fact that [] + (3, 5) will give me a
type error. Here I wonder why the augmented assignment behaves so much
different.

Can anyone help me understand this?

Thanks!

Uli
 
A

Alec Taylor

Quick aside, you can insert tuples without much effort: `points += ((3,5),)`

And also that I can't do the reverse, i.e.:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "list") to tuple

Hi everybody!

I was just smacked by some very surprising Python 2.7 behaviour. I was
assembling some 2D points into a list:

points = []
points += (3, 5)
points += (4, 6)

What I would have expected is to have [(3, 5), (4, 6)], instead I got [3,
5, 4, 6]. My interpretations thereof is that the tuple (x, y) is iterable,
so the elements are appended one after the other. Actually, I should have
used points.append(), but that's a different issue.

Now, what really struck me was the fact that [] + (3, 5) will give me a
type error. Here I wonder why the augmented assignment behaves so much
different.

Can anyone help me understand this?

Thanks!

Uli
 
D

Dave Angel

Hi everybody!

I was just smacked by some very surprising Python 2.7 behaviour. I was
assembling some 2D points into a list:

points = []
points += (3, 5)
points += (4, 6)

What I would have expected is to have [(3, 5), (4, 6)], instead I got [3,
5, 4, 6].

mylist +=
is equivalent to mylist.extend. And as you say, what you wanted was
append.

My interpretations thereof is that the tuple (x, y) is iterable,
You're confusing cause and effect. If it weren't iterable, it'd be an
error. It would NOT just somehow change to be equivalent to append.
Traceback (most recent call last):
so the elements are appended one after the other. Actually, I should have
used points.append(), but that's a different issue.

Now, what really struck me was the fact that [] + (3, 5) will give me a
type error. Here I wonder why the augmented assignment behaves so much
different.

What I wonder about is why list's __add__ is so fussy.
Can anyone help me understand this?

Thanks!

Uli
I'd also point out that when using the extend() function call, we'd have
to spell it:

points.extend((3,5))

The extra parentheses are to make it clear to the compiler that this is
a single argument, a tuple, and not two arguments.

And similarly,
points.append((3,5))
to get your original desired behavior.
 
T

Terry Reedy

What I wonder about is why list's __add__ is so fussy.

Guido's reason is that it is not clear what the types of [1,2] + (3,4),
(1,2) + [3,4], [] + range(4), range(2) + [3,4], etcetera should be. Such
mixtures may be bugs. Seq.__add__ exists to implement '+'.

[].extend() will clearly be a list, and accepts any iterable.

I believe the same logic is used for set operations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top