List comprehensions' ugliness (Was: Re: How to explain exactly what "def" does?)

Jp Calderone exarkun at intarweb.us
Fri Feb 7 13:04:31 EST 2003


On Fri, Feb 07, 2003 at 08:47:00AM -0800, J.Jacob wrote:
> [Tim Peters]
> > It has different semantics.  If it's your intent to extend a list in-place,
> > then .extend or += are necessary.  If it's your intent to create a new list
> > object, then .extend and += must not be used.  If it doesn't matter to your
> > app whether you create a new object, suit yourself.
> 
> I find it hard to believe that ``listx = listx + thing`` does not have
> the same semantics as ``listx += thing``.  I have not followed all the
> fancy syntax extensions, partly because I have to use Python 1.5.2
> often.  But what is going on at python-dev?  Do they really want all
> people coming from C++ to fall into this ``+=`` trap?
> 

  Apparently so.  I, for one, err on the side of readability and caution,
and recommend that no one ever use `+=' in their programs >:)

  If you find the unpredictable semantics of += for lists to be bad, I
imagine you won't like this, either:

  x = ([],)
  x[0] += [1]

  Be sure to examine x again, after the second line executes ;)

  Jp

-- 
There are 10 kinds of people: those who understand binary and those who do
not.
-- 
 up 53 days, 21:50, 7 users, load average: 0.16, 0.25, 0.22
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030207/f0872457/attachment.sig>


More information about the Python-list mailing list