append on lists
Duncan Booth
duncan.booth at invalid.invalid
Tue Sep 16 04:55:36 EDT 2008
"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.
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
More information about the Python-list
mailing list