append on lists
J. Cliff Dyer
jcd at sdf.lonestar.org
Tue Sep 16 15:07:57 EDT 2008
On Tue, 2008-09-16 at 08:49 -0500, Grant Edwards wrote:
> On 2008-09-16, Hrvoje Niksic <hniksic at xemacs.org> wrote:
> > Duncan Booth <duncan.booth at 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).
>
> It's also somewhat common to index into a literal:
>
> ['one','two','three'][n]
>
> {1:'one', 2:'two', 7:'seven'}[n]
>
Indeed. I use this for setting variables based on hostname on a
website. Say, for example, you run on port 80 on one host, but on a
test host, you share with several other sites, and run on 8084, you can
do the following
import socket
port = {
'www.example.com': 80,
'dev.example.com': 8084,
}[socket.gethostname()]
It's a handy idiom.
Cheers,
Cliff
More information about the Python-list
mailing list