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


Groups > comp.lang.python > #32730

Re: surprising += for lists

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.datemas.de!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'compiler': 0.05; 'say,': 0.05; 'python': 0.09; '5),': 0.09; 'argument,': 0.09; 'augmented': 0.09; 'effect.': 0.09; 'everybody!': 0.09; 'tuple': 0.09; 'typeerror:': 0.09; 'cc:addr:python-list': 0.10; '2.7': 0.13; "'int'": 0.16; '(3,': 0.16; '(x,': 0.16; 'behaviour.': 0.16; 'eckhardt': 0.16; 'iterable': 0.16; 'iterable,': 0.16; 'mylist': 0.16; 'parentheses': 0.16; 'similarly,': 0.16; 'surprising': 0.16; 'tuple,': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'equivalent': 0.20; 'issue.': 0.20; 'error.': 0.21; '"",': 0.22; 'assignment': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'elements': 0.23; 'cc:no real name:2**0': 0.24; "we'd": 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'thanks!': 0.26; '(most': 0.27; 'am,': 0.27; 'wonder': 0.27; 'list:': 0.27; 'this?': 0.28; 'arguments.': 0.29; 'behavior.': 0.29; 'points': 0.29; 'function': 0.30; 'point': 0.31; 'file': 0.32; 'subject:lists': 0.32; 'traceback': 0.33; 'anyone': 0.33; 'clear': 0.35; 'expected': 0.35; 'really': 0.36; 'but': 0.36; 'wanted': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'two': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'fact': 0.38; 'object': 0.38; 'some': 0.38; 'instead': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'your': 0.60; 'different': 0.63; 'other.': 0.64; 'here': 0.65; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'actually,': 0.84; 'confusing': 0.84; 'different.': 0.84; "it'd": 0.84; 'thereof': 0.84; 'spell': 0.91
Date Sun, 04 Nov 2012 07:45:45 -0500
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120912 Thunderbird/15.0.1
MIME-Version 1.0
To Ulrich Eckhardt <doomster@knuut.de>
Subject Re: surprising += for lists
References <afn3i8Fn4j1U1@mid.uni-berlin.de>
In-Reply-To <afn3i8Fn4j1U1@mid.uni-berlin.de>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:0k/Eo4gFQKUCI3nx3stos3hwJT/xASKCcJlLr35KN4p UIzeMNjTbdgwzIe7GMrWD+hC/aY8XdJPJkbAJxaliY18FkVG6+ m6I0M/1SIQrgLJvn8R9hvirwTdshLG6XmyGQZZHMWQHYnvQjd7 lFGDoi10Tny/dKiqOLWPv76TZzcOY3/Q1y/nfU3g9Pc/hV3grN c0KFMm3a/veoIQKt0JcN+pJm46O6/ALOQH9h00sQS3n/u8ikrY bS63tEsvmvVOI92mKsbw17id4zkylQRG7WYofcCrD8g9wy3VbC 9SrrCokv+u/eUKZYsDNblEkzuCI+s3nfwwKtoiJtmGmRg1A0g= =
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
Reply-To d@davea.name
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3256.1352033476.27098.python-list@python.org> (permalink)
Lines 60
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1352033476 news.xs4all.nl 6969 [2001:888:2000:d::a6]:39839
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32730

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web