append on lists

Grant Edwards grante at visi.com
Tue Sep 16 09:54:39 EDT 2008


On 2008-09-16, Armin <a at nospam.org> wrote:
> Duncan Booth wrote:
>> "Chris Rebert" <clp at rebertia.com> wrote:
>>> On Tue, Sep 16, 2008 at 1:20 AM, Armin <a at nospam.org> wrote:
>>>>  [1,2,3,4,7].append(c) -> Is this a valid expression?
>>> Literally, no, because you can't call methods on literals.
>> 
>> Rubbish. There is no restriction about calling methods on literals. That 
>> expression is perfectly valid but has no practical use that I can see.
>
> The semantic of [1,2,3,4,7].append(c) and [1,2,3,4,7] + c
> (with c = [8,9]) is identical,

No, they're not:

>>> a=[1,2,3,4,7]
>>> c=[9,10]
>>> a.append(c)
>>> a
[1, 2, 3, 4, 7, [9, 10]]


>>> a=[1,2,3,4,7]
>>> c=[9,10]
>>> a+c
[1, 2, 3, 4, 7, 9, 10]

You really ought to install a copy of Python so you can try out
things and see how they really work.

> but the first expression doesn't provide a value. Strange by
> design ...

Not really.  You just need to make a little effort to
understand the reasoning (which has been explained to you)
behind Python's design decision.

-- 
Grant Edwards                   grante             Yow! Somewhere in DOWNTOWN
                                  at               BURBANK a prostitute is
                               visi.com            OVERCOOKING a LAMB CHOP!!



More information about the Python-list mailing list