List comprehensions' ugliness

Mel Wilson mwilson at the-wire.com
Fri Feb 7 15:02:11 EST 2003


In article <13285ea2.0302070846.5deca6c6 at posting.google.com>,
joost_jacob at hotmail.com (J.Jacob) wrote:
>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?

They only differ in a very Pythonic way ..

Python 2.1.3 (#35, Apr  8 2002, 17:47:50) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> M = [1,2,3]
>>> M1 = M
>>> M = M + [4]
>>> M
[1, 2, 3, 4]
>>> M1
[1, 2, 3]
>>>
>>> N = [1,2,3]
>>> N1 = N
>>> N += [4]
>>> N
[1, 2, 3, 4]
>>> N1
[1, 2, 3, 4]
>>>

   Note that the effects on M and N are just what you would
expect from C++ semantics.  The effects on M1 and N1 are
different, but an ex-C++ programmer is going to have to get
used to this in Python, for lots of other reasons.
References are ubiquitous in Python.  M and M1 start by
referring to the same object, but the assignment refers M to
a different one.  N and N1 refer to the same object
throughout.

        Regards.        Mel.




More information about the Python-list mailing list