On Mon, Sep 15, 2008 at 1:24 PM, Armin <a@nospam.orgwr ote:
>
>
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.or gwrote:
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)'.
On Mon, Sep 15, 2008 at 1:24 PM, Armin <a@nospam.orgwr ote:
>>
>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
>
If you print 'a' after doing the .append(), you'll see it's changed to
your desired value.
>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 Sep 16, 6:45Â am, Armin <a...@nospam.or gwrote:
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?
>>>
>>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.
Regards,
Chris
>
--Armin
>
>
>>
>If you print 'a' after doing the .append(), you'll see it's changed to
>your desired value.
On Mon, Sep 15, 2008 at 4:45 PM, Armin <a@nospam.orgwr ote:
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?
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:
On Mon, Sep 15, 2008 at 4:03 PM, Steven D'Aprano
<steve@remove-this-cybersource.com .auwrote:
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:
>
>
Comment