Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
append on lists
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 48 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Armin  
View profile  
 More options Sep 15 2008, 4:24 pm
Newsgroups: comp.lang.python
From: Armin <a...@nospam.org>
Date: Mon, 15 Sep 2008 22:24:39 +0200
Local: Mon, Sep 15 2008 4:24 pm
Subject: append on lists

Hi,

just a dumb question.

Let a = [1,2,3,4,5]

Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??

--Armin


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fredrik Lundh  
View profile  
 More options Sep 15 2008, 4:17 pm
Newsgroups: comp.lang.python
From: Fredrik Lundh <fred...@pythonware.com>
Date: Mon, 15 Sep 2008 22:17:37 +0200
Local: Mon, Sep 15 2008 4:17 pm
Subject: Re: append on lists

Armin wrote:
> just a dumb question.

> Let a = [1,2,3,4,5]

> Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??

yeah, that's a dumb question.

</F>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Armin  
View profile  
 More options Sep 15 2008, 4:35 pm
Newsgroups: comp.lang.python
From: Armin <a...@nospam.org>
Date: Mon, 15 Sep 2008 22:35:36 +0200
Local: Mon, Sep 15 2008 4:35 pm
Subject: Re: append on lists

Fredrik Lundh wrote:
> Armin wrote:

>> just a dumb question.

>> Let a = [1,2,3,4,5]

>> Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??

> yeah, that's a dumb question.

> </F>

  yeah, that's a dumb answer.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Sep 15 2008, 4:21 pm
Newsgroups: comp.lang.python
From: "Chris Rebert" <cvreb...@gmail.com>
Date: Mon, 15 Sep 2008 13:21:31 -0700
Local: Mon, Sep 15 2008 4:21 pm
Subject: Re: append on lists
On Mon, Sep 15, 2008 at 1:24 PM, Armin <a...@nospam.org> wrote:

> Hi,

> just a dumb question.

> Let a = [1,2,3,4,5]

> Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??

I'll assume the presence of the 6 is a typo.

Because .append() mutates 'a' and appends the item in-place rather
than creating and returning a new list with the item appended, and
it's good Python style for mutating methods to have no return value
(since all functions must have some return value, Python uses None
when the function doesn't explicitly return anything).

If you print 'a' after doing the .append(), you'll see it's changed to
your desired value.

Regards,
Chris

--
Follow the path of the Iguana...
http://rebertia.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Arnaud Delobelle  
View profile  
 More options Sep 15 2008, 4:22 pm
Newsgroups: comp.lang.python
From: Arnaud Delobelle <arno...@googlemail.com>
Date: Mon, 15 Sep 2008 13:22:06 -0700 (PDT)
Local: Mon, Sep 15 2008 4:22 pm
Subject: Re: append on lists
On Sep 15, 9:24 pm, Armin <a...@nospam.org> wrote:

> Hi,

> just a dumb question.

> Let a = [1,2,3,4,5]

> Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??

> --Armin

Because list.append is a method that mutates its object, and such
method usually return None.  What you should check is the value of 'a'
after 'a.append(7)'.

--
Arnaud


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jerry Hill  
View profile  
 More options Sep 15 2008, 4:23 pm
Newsgroups: comp.lang.python
From: "Jerry Hill" <malaclyp...@gmail.com>
Date: Mon, 15 Sep 2008 16:23:51 -0400
Local: Mon, Sep 15 2008 4:23 pm
Subject: Re: append on lists

On Mon, Sep 15, 2008 at 4:24 PM, Armin <a...@nospam.org> wrote:
> Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??

Because the list a has been altered in place.

--
Jerry


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Armin  
View profile  
 More options Sep 15 2008, 4:45 pm
Newsgroups: comp.lang.python
From: Armin <a...@nospam.org>
Date: Mon, 15 Sep 2008 22:45:38 +0200
Local: Mon, Sep 15 2008 4:45 pm
Subject: Re: append on lists

Chris Rebert wrote:
> On Mon, Sep 15, 2008 at 1:24 PM, Armin <a...@nospam.org> wrote:

>> Hi,

>> just a dumb question.

>> Let a = [1,2,3,4,5]

>> Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??
> I'll assume the presence of the 6 is a typo.

Sorry, that's the case.

> Because .append() mutates 'a' and appends the item in-place rather
> than creating and returning a new list with the item appended, and
> it's good Python style for mutating methods to have no return value
> (since all functions must have some return value, Python uses None
> when the function doesn't explicitly return anything).

Yes, but this is very unconvenient.
If d should reference the list a extended with a single list element
you need at least two lines

a.append(7)
d=a

and not more intuitive d = a.append(7)

--Armin


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fredrik Lundh  
View profile  
 More options Sep 15 2008, 4:32 pm
Newsgroups: comp.lang.python
From: Fredrik Lundh <fred...@pythonware.com>
Date: Mon, 15 Sep 2008 22:32:18 +0200
Local: Mon, Sep 15 2008 4:32 pm
Subject: Re: append on lists

Armin wrote:
>>> just a dumb question.

>>> Let a = [1,2,3,4,5]

>>> Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??

>> yeah, that's a dumb question.

> yeah, that's a dumb answer.

did you read your own post?  I did.

</F>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Grant Edwards  
View profile  
 More options Sep 15 2008, 4:40 pm
Newsgroups: comp.lang.python
From: Grant Edwards <invalid@invalid>
Date: Mon, 15 Sep 2008 15:40:50 -0500
Local: Mon, Sep 15 2008 4:40 pm
Subject: Re: append on lists
On 2008-09-15, Armin <a...@nospam.org> wrote:

>> Because .append() mutates 'a' and appends the item in-place rather
>> than creating and returning a new list with the item appended, and
>> it's good Python style for mutating methods to have no return value
>> (since all functions must have some return value, Python uses None
>> when the function doesn't explicitly return anything).

> Yes, but this is very unconvenient.

Aw, admit it -- it's not _that_ inconvenient.

> If d should reference the list a extended with a single list
> element you need at least two lines

> a.append(7)
> d=a

With that usage it's obvious that a is mutated, and now both
"a" and "d" are bound to the same extended list.

> and not more intuitive d = a.append(7)

Becase that usage implies (at least to many of us) that "a" is
unchanged, and that "d" is now bound to a different object than
"a".

--
Grant Edwards                   grante             Yow! Where's th' DAFFY
                                  at               DUCK EXHIBIT??
                               visi.com            


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fredrik Lundh  
View profile  
 More options Sep 15 2008, 4:43 pm
Newsgroups: comp.lang.python
From: Fredrik Lundh <fred...@pythonware.com>
Date: Mon, 15 Sep 2008 22:43:00 +0200
Subject: Re: append on lists

Armin wrote:

 > If d should reference the list a extended with a single list element
 > you need at least two lines
 >
 > a.append(7)
 > d=a

why do you need two names for the same thing?

> and not more intuitive d = a.append(7)

unless you completely change the semantics of "append", your code would
modify "a" as well.  how is that "more intuitive"?

side effects are bad as they are, but side effects in unexpected places
is a really lousy idea.  I don't think you've thought this one through,
really.

</F>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Machin  
View profile  
 More options Sep 15 2008, 4:47 pm
Newsgroups: comp.lang.python
From: John Machin <sjmac...@lexicon.net>
Date: Mon, 15 Sep 2008 13:47:24 -0700 (PDT)
Local: Mon, Sep 15 2008 4:47 pm
Subject: Re: append on lists
On Sep 16, 6:45 am, Armin <a...@nospam.org> wrote:

> Yes, but this is very unconvenient.
> If d should reference the list a extended with a single list element
> you need at least two lines

> a.append(7)
> d=a

> and not more intuitive d = a.append(7)

Methods/functions which return a value other than the formal None and
also mutate their environment are "a snare and a delusion". Don't wish
for them.

Inconvenient? How often do you want to mutate a list and then set up
another reference to it?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Sep 15 2008, 4:47 pm
Newsgroups: comp.lang.python
From: "Chris Rebert" <c...@rebertia.com>
Date: Mon, 15 Sep 2008 13:47:53 -0700
Local: Mon, Sep 15 2008 4:47 pm
Subject: Re: append on lists

And then they'd both reference the same list and you'd run into all
sorts of problems.

The code you'd actually want is:

d = a[:] #copy a
d.append(7)

Or if you're willing to overlook the inefficiency:

d = a + [7]

But that's not idiomatic.

And debating the fundamentals of the language, which aren't going to
change anytime soon, isn't going to get you anywhere.
You may be interested in looking at Python's "tuple" datatype, which
is basically an immutable list.

I'd also suggest you Read The Fine Tutorial, and that your original
question was better suited to IRC or python-tutors
(http://mail.python.org/mailman/listinfo/tutor) than this mailinglist.

Regards,
Chris

--
Follow the path of the Iguana...
http://rebertia.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jerry Hill  
View profile  
 More options Sep 15 2008, 4:50 pm
Newsgroups: comp.lang.python
From: "Jerry Hill" <malaclyp...@gmail.com>
Date: Mon, 15 Sep 2008 16:50:18 -0400
Local: Mon, Sep 15 2008 4:50 pm
Subject: Re: append on lists

On Mon, Sep 15, 2008 at 4:45 PM, Armin <a...@nospam.org> wrote:
> Yes, but this is very unconvenient.
> If d should reference the list a extended with a single list element
> you need at least two lines

You do it in two lines, because you're doing two different things.

> a.append(7)

This appends the element 7 to the list a.

> d=a

This binds the name d to the same list as a is bound to.  If you wand
d to point to a new list with the same contents as the list a, plus
the number 7 do this:

d = a + [7]

Here's an example of the difference:

>>> a = range(6)
>>> a
[0, 1, 2, 3, 4, 5]
>>> a.append(7)
>>> a

[0, 1, 2, 3, 4, 5, 7]
>>> d = a
>>> d

[0, 1, 2, 3, 4, 5, 7]
>>> d.append(10)
>>> a

[0, 1, 2, 3, 4, 5, 7, 10]
>>> d

[0, 1, 2, 3, 4, 5, 7, 10]


See how a and d are two names bound to the same list?

Here's the other way:

>>> a = range(6)
>>> d = a + [7]
>>> a
[0, 1, 2, 3, 4, 5]
>>> d

[0, 1, 2, 3, 4, 5, 7]
>>> d.append(10)
>>> a
[0, 1, 2, 3, 4, 5]
>>> d

[0, 1, 2, 3, 4, 5, 7, 10]


--
Jerry

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Sep 15 2008, 7:03 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au>
Date: 15 Sep 2008 23:03:59 GMT
Local: Mon, Sep 15 2008 7:03 pm
Subject: Re: append on lists

On Mon, 15 Sep 2008 13:47:53 -0700, Chris Rebert wrote:
> The code you'd actually want is:

> d = a[:] #copy a
> d.append(7)

> Or if you're willing to overlook the inefficiency:

> d = a + [7]

> But that's not idiomatic.

Why is a + [7] more inefficient than manually copying the list and
appending to the copy? Surely both pieces of code end up doing the same
thing?

In fact, I'd guess that the second is likely to be marginally more
efficient than the first:

>>> x = compile('d = a[:]; d.append(7)', '', 'exec')
>>> dis.dis(x)

  1           0 LOAD_NAME                0 (a)
              3 SLICE+0
              4 STORE_NAME               1 (d)
              7 LOAD_NAME                1 (d)
             10 LOAD_ATTR                2 (append)
             13 LOAD_CONST               0 (7)
             16 CALL_FUNCTION            1
             19 POP_TOP
             20 LOAD_CONST               1 (None)
             23 RETURN_VALUE

>>> x = compile('d = a + [7]', '', 'exec')
>>> dis.dis(x)

  1           0 LOAD_NAME                0 (a)
              3 LOAD_CONST               0 (7)
              6 BUILD_LIST               1
              9 BINARY_ADD
             10 STORE_NAME               1 (d)
             13 LOAD_CONST               1 (None)
             16 RETURN_VALUE

timeit agrees with me:

>>> from timeit import Timer
>>> t1 = Timer('d = a[:]; d.append(7)', 'a = []')
>>> t2 = Timer('d = a + [7]', 'a = []')
>>> t1.repeat(number=1000)

[0.0015339851379394531, 0.0014910697937011719, 0.0014841556549072266]
>>> t2.repeat(number=1000)

[0.0011889934539794922, 0.0013048648834228516, 0.0013070106506347656]

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Sep 15 2008, 8:08 pm
Newsgroups: comp.lang.python
From: "Chris Rebert" <c...@rebertia.com>
Date: Mon, 15 Sep 2008 17:08:30 -0700
Local: Mon, Sep 15 2008 8:08 pm
Subject: Re: append on lists
On Mon, Sep 15, 2008 at 4:03 PM, Steven D'Aprano

Sorry, I was just speculating based on the extraneous list ([7]) used
in the second one.
My bad. :)

Regards,
Chris

--
Follow the path of the Iguana...
http://rebertia.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Anderson  
View profile  
 More options Sep 16 2008, 1:03 am
Newsgroups: comp.lang.python
From: Peter Anderson <peter.ander...@internode.on.net>
Date: Tue, 16 Sep 2008 15:03:51 +1000
Local: Tues, Sep 16 2008 1:03 am
Subject: Re: append on lists
"/... I don't think you've thought this one through, really./"

Is this kind of response really necessary? The original post was asking
(from his perspective at least) a legitimate question. To a Python
expert it may be a "silly question" but replying in such a fatuous way
puts off those "less gifted" from using this excellent reference source.
Nobody enjoys being spoken to like this.

We ought to try and be a little kinder to others on the list, don't you
think? :-)

Regards,
Peter
--
*Peter Anderson*
There is nothing more difficult to take in hand, more perilous to
conduct, or more uncertain in its success, than to take the lead in the
introduction of a new order of things—Niccolo Machiavelli, /The Prince/,
ch. 6


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
tony.clar...@googlemail.com  
View profile  
 More options Sep 16 2008, 1:58 am
Newsgroups: comp.lang.python
From: tony.clar...@googlemail.com
Date: Mon, 15 Sep 2008 22:58:10 -0700 (PDT)
Local: Tues, Sep 16 2008 1:58 am
Subject: Re: append on lists
On Sep 16, 6:03 am, Peter Anderson <peter.ander...@internode.on.net>
wrote:

> "/... I don't think you've thought this one through, really./"
> snip

> We ought to try and be a little kinder to others on the list, don't you
> think? :-)

> snip

Well said!
Tony

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Armin  
View profile  
 More options Sep 16 2008, 4:20 am
Newsgroups: comp.lang.python
From: Armin <a...@nospam.org>
Date: Tue, 16 Sep 2008 10:20:19 +0200
Local: Tues, Sep 16 2008 4:20 am
Subject: Re: append on lists

John Machin wrote:
> On Sep 16, 6:45 am, Armin <a...@nospam.org> wrote:

>> Yes, but this is very unconvenient.
>> If d should reference the list a extended with a single list element
>> you need at least two lines

>> a.append(7)
>> d=a

>> and not more intuitive d = a.append(7)

> Methods/functions which return a value other than the formal None and
> also mutate their environment are "a snare and a delusion". Don't wish
> for them.

   c = [9,10]
   [1,2,3,4,7].append(c) -> Is this a valid expression?

   The 'value' of that expression is None.

   However ... that's the way of the implementation of the append method.
   It's a little bit confusing to me ...

--Armin

Thanks to all !


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Sep 16 2008, 4:15 am
Newsgroups: comp.lang.python
From: "Chris Rebert" <c...@rebertia.com>
Date: Tue, 16 Sep 2008 01:15:08 -0700
Local: Tues, Sep 16 2008 4:15 am
Subject: Re: append on lists

Literally, no, because you can't call methods on literals.
However, the sentiment is valid, though probably not what you want:

>>> c = [9,10]
>>> a = [1,2,3,4,7]
>>> b = a[:]
>>> a.append(c)
>>> a #note the nested list

[1, 2, 3, 4, 7, [9, 10]]
>>> b
[1, 2, 3, 4, 7]
>>> b.extend(c)
>>> b

[1, 2, 3, 4, 7, 9, 10]

Regards,
Chris

--
Follow the path of the Iguana...
http://rebertia.com

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hrvoje Niksic  
View profile  
 More options Sep 16 2008, 4:48 am
Newsgroups: comp.lang.python
From: Hrvoje Niksic <hnik...@xemacs.org>
Date: Tue, 16 Sep 2008 10:48:42 +0200
Local: Tues, Sep 16 2008 4:48 am
Subject: Re: append on lists

"Chris Rebert" <c...@rebertia.com> writes:
>>  c = [9,10]
>>  [1,2,3,4,7].append(c) -> Is this a valid expression?

> Literally, no, because you can't call methods on literals.

This is in fact not true.  [1,2,3,4,7].append([9, 10]) is a perfectly
valid expression, only it doesn't do much (that you can observe).

The canonical response (no doubt already quoted in this thread) is
that returning the list from append would confuse the hell out of
people who expect a copy of the list, such as:

a = [1, 2, 3]
b = a.append(4)
# if the above "worked" in the sense that b == [1, 2, 3, 4], I for one
# would expect a to remain unchanged


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Duncan Booth  
View profile  
 More options Sep 16 2008, 4:55 am
Newsgroups: comp.lang.python
From: Duncan Booth <duncan.bo...@invalid.invalid>
Date: 16 Sep 2008 08:55:36 GMT
Local: Tues, Sep 16 2008 4:55 am
Subject: Re: append on lists

"Chris Rebert" <c...@rebertia.com> wrote:
> On Tue, Sep 16, 2008 at 1:20 AM, Armin <a...@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.

There is a syntax gotcha which you may have been thinking of: to call a
method on an integer literal (or indeed to access any attribute) you have
to use whitespace between the literal and the dot otherwise you have a
float literal and a syntax error.

>>> 5 .__hex__()

'0x5'

The only relatively common use I can think of where you might want to call
a method directly on a literal is to produce a list of strings while being
lazy about the typing:

COLOURS = "red green blue pink yellow".split()

versus

COLOURS = ["red", "green", "blue", "pink", "yellow"]

--
Duncan Booth http://kupuguy.blogspot.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hrvoje Niksic  
View profile  
 More options Sep 16 2008, 5:03 am
Newsgroups: comp.lang.python
From: Hrvoje Niksic <hnik...@xemacs.org>
Date: Tue, 16 Sep 2008 11:03:59 +0200
Local: Tues, Sep 16 2008 5:03 am
Subject: Re: append on lists

Duncan Booth <duncan.bo...@invalid.invalid> writes:
> The only relatively common use I can think of where you might want to call
> a method directly on a literal is to produce a list of strings while being
> lazy about the typing:

By far the most common is probably 'sep'.join(iterable).

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Marandon  
View profile  
 More options Sep 16 2008, 6:15 am
Newsgroups: comp.lang.python
From: Alex Marandon <inva...@nowhere.invalid.org>
Date: Tue, 16 Sep 2008 11:15:48 +0100
Local: Tues, Sep 16 2008 6:15 am
Subject: Re: append on lists

Hi,

You might be interested in using the + operator instead of append. You
could also define your own list type, based on the UserList included in
the standard library.

 >>> from UserList import UserList
 >>> class MyList(UserList):
...     def my_append(self, value):
...         return self + [value]
...
 >>> l = MyList([1,2,3,4])
 >>> l
[1, 2, 3, 4]
 >>> l.my_append(5)
[1, 2, 3, 4, 5]
 >>> l
[1, 2, 3, 4]


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Armin  
View profile  
 More options Sep 16 2008, 7:10 am
Newsgroups: comp.lang.python
From: Armin <a...@nospam.org>
Date: Tue, 16 Sep 2008 13:10:37 +0200
Local: Tues, Sep 16 2008 7:10 am
Subject: Re: append on lists

Duncan Booth wrote:
> "Chris Rebert" <c...@rebertia.com> wrote:
>> On Tue, Sep 16, 2008 at 1:20 AM, Armin <a...@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, but the first expression doesn't provide
a value. Strange by design ...

--Armin


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Marandon  
View profile  
 More options Sep 16 2008, 7:22 am
Newsgroups: comp.lang.python
From: Alex Marandon <inva...@nowhere.invalid.org>
Date: Tue, 16 Sep 2008 12:22:46 +0100
Local: Tues, Sep 16 2008 7:22 am
Subject: Re: append on lists

Armin wrote:
> Duncan Booth wrote:

> The semantic of [1,2,3,4,7].append(c) and [1,2,3,4,7] + c
> (with c = [8,9]) is identical,

No it's not, + doesn't alter its operands.

 >>> a = 1
 >>> b = 2
 >>> a + b
3
 >>> a
1

Were you expecting a == 3?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 48   Newer >
« Back to Discussions « Newer topic     Older topic »