Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91845
| From | Laura Creighton <lac@openend.se> |
|---|---|
| Subject | Re: for...else |
| References | <CANFcO8tOLorV1kuGJo6Vgb-e02EhC3NL2U7oiNRNF+b2s2M2ig@mail.gmail.com> <1831776353.1951415.1433246463232.JavaMail.root@sequans.com><CANFcO8sQ0BjHU+7i6XEpaygZLhhi2R0zh0enNk4zk=L8QT3=kg@mail.gmail.com> |
| Date | 2015-06-02 16:29 +0200 |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.64.1433255400.13271.python-list@python.org> (permalink) |
You may be looking for dictionary dispatching.
You can translate the key into a callable.
def do_ping(self, arg):
return 'Ping, {0}!'.format(arg)
def do_pong(self, arg):
return 'Pong, {0}!'.format(arg)
dispatch = {
'ping': do_ping,
'pong': do_pong
}
and then at some point do
dispatch[command](arg)
It is useful to know that tuples make perfectly good dictionary keys,
in case you need to store more state somewhere.
Laura
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: for...else Laura Creighton <lac@openend.se> - 2015-06-02 16:29 +0200
Re: for...else "ast" <nomail@invalid.com> - 2015-06-02 19:46 +0200
Re: for...else Laura Creighton <lac@openend.se> - 2015-06-02 20:16 +0200
csiph-web