Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32727 > unrolled thread

surprising += for lists

Started byUlrich Eckhardt <doomster@knuut.de>
First post2012-11-04 12:57 +0100
Last post2012-11-04 12:57 -0500
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  surprising += for lists Ulrich Eckhardt <doomster@knuut.de> - 2012-11-04 12:57 +0100
    Re: surprising += for lists Alec Taylor <alec.taylor6@gmail.com> - 2012-11-04 23:06 +1100
    Re: surprising += for lists Dave Angel <d@davea.name> - 2012-11-04 07:45 -0500
    Re: surprising += for lists Terry Reedy <tjreedy@udel.edu> - 2012-11-04 12:57 -0500

#32727 — surprising += for lists

FromUlrich Eckhardt <doomster@knuut.de>
Date2012-11-04 12:57 +0100
Subjectsurprising += for lists
Message-ID<afn3i8Fn4j1U1@mid.uni-berlin.de>
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

[toc] | [next] | [standalone]


#32728

FromAlec Taylor <alec.taylor6@gmail.com>
Date2012-11-04 23:06 +1100
Message-ID<mailman.3254.1352030765.27098.python-list@python.org>
In reply to#32727
Quick aside, you can insert tuples without much effort: `points += ((3,5),)`

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

On Sun, Nov 4, 2012 at 10:57 PM, Ulrich Eckhardt <doomster@knuut.de> wrote:
> 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
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list

[toc] | [prev] | [next] | [standalone]


#32730

FromDave Angel <d@davea.name>
Date2012-11-04 07:45 -0500
Message-ID<mailman.3256.1352033476.27098.python-list@python.org>
In reply to#32727
On 11/04/2012 06:57 AM, Ulrich Eckhardt wrote:
> 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.

>>> points.extend(4)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not 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.

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.


-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#32738

FromTerry Reedy <tjreedy@udel.edu>
Date2012-11-04 12:57 -0500
Message-ID<mailman.3260.1352051847.27098.python-list@python.org>
In reply to#32727
On 11/4/2012 7:45 AM, Dave Angel wrote:

> 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.

-- 
Terry Jan Reedy

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web