Augmented augmented assignment?
David Bolen
db3l at fitlinxx.com
Mon Oct 22 18:43:37 EDT 2001
Dale Strickland-Clark <dale at riverhall.NOTHANKS.co.uk> writes:
> I assume it's something to do with the in-place operation of augmented
> assignment but I can't seem to reproduce it using normal assignment.
>
> >>> x = 'spam'
> >>> y = []
> >>> y = y + x
> Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
> TypeError: can only concatenate list (not "string") to list
> >>> y += x
> >>> y
> ['s', 'p', 'a', 'm']
It looks like between 1.5.2 and 2.0, the extend() method was adjusted
to support any sequence. Doing y.extend('spam') accomplishes the same
thing as the augmented assignment - probably what's happening behind
the scenes - whereas in 1.5.2, y.extend('spam') generated a TypeError,
and wants a list.
It does seem like an undesirable inconsistency though, since it's only
with a mutable list that it happens (you get the same concatenation
TypeError in either case if y is a tuple).
--
-- David
--
/-----------------------------------------------------------------------\
\ David Bolen \ E-mail: db3l at fitlinxx.com /
| FitLinxx, Inc. \ Phone: (203) 708-5192 |
/ 860 Canal Street, Stamford, CT 06902 \ Fax: (203) 316-5150 \
\-----------------------------------------------------------------------/
More information about the Python-list
mailing list