append on lists
a at nospam.org
Tue Sep 16 13:10:37 CEST 2008
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, 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__()
> 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()
> COLOURS = ["red", "green", "blue", "pink", "yellow"]
More information about the Python-list