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.
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)'.
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
>> 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
On Mon, Sep 15, 2008 at 1:45 PM, Armin <a...@nospam.org> wrote: > 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)
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.
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
<st...@remove-this-cybersource.com.au> wrote: > 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:
"/... 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
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 ...
On Tue, Sep 16, 2008 at 1:20 AM, Armin <a...@nospam.org> wrote: > 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?
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
"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
"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:
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).
Armin wrote: > 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] ??
>> 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.
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]
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 ...
> 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: