F
Fredrik Lundh
Armin said: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>
Armin said: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.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] ??
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
Why is the value of a.append(7) equal None and not [1,2,3,4,5,6,7] ??
Armin said: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.
Fredrik said:Armin said: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>
Armin said:> 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)
Chris said:I'll assume the presence of the 6 is a typo.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.
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)
Chris said: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)
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)
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]
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]
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.
1 0 LOAD_NAME 0 (a)x = compile('d = a[:]; d.append(7)', '', 'exec')
dis.dis(x)
1 0 LOAD_NAME 0 (a)x = compile('d = a + [7]', '', 'exec')
dis.dis(x)
[0.0011889934539794922, 0.0013048648834228516, 0.0013070106506347656]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)
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:
1 0 LOAD_NAME 0 (a)x = compile('d = a[:]; d.append(7)', '', 'exec')
dis.dis(x)
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
1 0 LOAD_NAME 0 (a)x = compile('d = a + [7]', '', 'exec')
dis.dis(x)
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:
[0.0011889934539794922, 0.0013048648834228516, 0.0013070106506347656]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)
John said: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?
[1, 2, 3, 4, 7, 9, 10]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
John said: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.
Armin said:Chris said: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.
[1, 2, 3, 4]>>> l = MyList([1,2,3,4])
>>> l [1, 2, 3, 4]
>>> l.my_append(5) [1, 2, 3, 4, 5]
>>> l
Duncan said:Chris Rebert said:Literally, no, because you can't call methods on literals.[1,2,3,4,7].append(c) -> Is this a valid expression?
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.
'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"]
Armin said: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,
Alex said:Armin said: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.
3
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.