Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91845

Re: for...else

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!news.osn.de!diablo1.news.osn.de!137.226.75.22.MISMATCH!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <lac@openend.se>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'keys,': 0.07; 'callable.': 0.09; 'received:openend.se': 0.09; 'received:theraft.openend.se': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.14; 'translate': 0.15; 'arg):': 0.16; 'cc:addr:lac': 0.16; 'cc:addr:openend.se': 0.16; 'from:addr:lac': 0.16; 'from:addr:openend.se': 0.16; 'from:name:laura creighton': 0.16; 'message-id:@fido.openend.se': 0.16; 'received:89.233': 0.16; 'received:89.233.217': 0.16; 'received:89.233.217.133': 0.16; 'received:fido': 0.16; 'received:fido.openend.se': 0.16; 'somewhere.': 0.16; 'laura': 0.18; 'cc:2**2': 0.20; 'cc:addr:python.org': 0.21; 'tuples': 0.22; 'header:In-Reply-To:1': 0.24; 'dictionary': 0.29; 'received:se': 0.29; 'cc:no real name:2**1': 0.29; 'skip:d 20': 0.32; 'point': 0.33; 'useful': 0.35; 'skip:{ 10': 0.36; 'subject:: ': 0.37; 'charset:us-ascii': 0.37; 'some': 0.40; 'header:Message-Id:1': 0.62; 'more': 0.62; 'received:89': 0.80
To acdr <mail.acdr@gmail.com>
cc Jean-Michel Pichavant <jeanmichel@sequans.com>, python-list@python.org, lac@openend.se
From Laura Creighton <lac@openend.se>
Subject Re: for...else
In-Reply-To Message from acdr <mail.acdr@gmail.com> of "Tue, 02 Jun 2015 14:52:21 +0200." <CANFcO8sQ0BjHU+7i6XEpaygZLhhi2R0zh0enNk4zk=L8QT3=kg@mail.gmail.com>
References <CANFcO8tOLorV1kuGJo6Vgb-e02EhC3NL2U7oiNRNF+b2s2M2ig@mail.gmail.com> <1831776353.1951415.1433246463232.JavaMail.root@sequans.com><CANFcO8sQ0BjHU+7i6XEpaygZLhhi2R0zh0enNk4zk=L8QT3=kg@mail.gmail.com>
MIME-Version 1.0
Content-Type text/plain; charset="us-ascii"
Content-ID <30674.1433255390.1@fido>
Date Tue, 02 Jun 2015 16:29:50 +0200
X-Greylist Sender IP whitelisted, not delayed by milter-greylist-4.3.9 (theraft.openend.se [89.233.217.130]); Tue, 02 Jun 2015 16:29:52 +0200 (CEST)
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.64.1433255400.13271.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1433255400 news.xs4all.nl 2825 [2001:888:2000:d::a6]:33980
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:91845

Show key headers only | View raw


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 | NextNext in thread | Find similar | Unroll thread


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