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


Groups > comp.lang.python > #41041

Re: Switch statement

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'args': 0.04; 'paths': 0.05; 'versions.': 0.07; 'arg': 0.09; 'arguments,': 0.09; 'if,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'cleanly': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'statement.': 0.16; 'wrote:': 0.17; 'instance,': 0.17; 'jan': 0.18; 'otherwise,': 0.20; 'earlier': 0.21; 'object.': 0.22; 'statement': 0.23; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'am,': 0.27; 'header:X-Complaints-To:1': 0.28; 'arguments.': 0.29; "d'aprano": 0.29; 'steven': 0.29; 'function': 0.30; 'switch': 0.32; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'requiring': 0.35; 'there': 0.35; 'received:org': 0.36; 'ability': 0.36; 'signature': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'relatively': 0.62; 'provide': 0.62; 'more': 0.63; '2013': 0.84; 'received:fios.verizon.net': 0.84; 'same,': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: Switch statement
Date Sun, 10 Mar 2013 18:02:25 -0400
References <mailman.3155.1362925062.2939.python-list@python.org> <513ca430$0$6512$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130215 Thunderbird/17.0.3
In-Reply-To <513ca430$0$6512$c3e8da3$5496439d@news.astraweb.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3170.1362952971.2939.python-list@python.org> (permalink)
Lines 49
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1362952971 news.xs4all.nl 6926 [2001:888:2000:d::a6]:53519
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41041

Show key headers only | View raw


On 3/10/2013 11:18 AM, Steven D'Aprano wrote:
> On Sun, 10 Mar 2013 14:16:27 +0000, Joseph L. Casale wrote:
>
>> I have a switch statement composed using a dict:
>>
>>
>> switch = {
>>      'a': func_a,
>>      'b': func_b,
>>      'c': func_c
>> }
>> switch.get(var, default)()
>>
>>
>> As a result of multiple functions per choice, it migrated to:
>>
>>
>>
>> switch = {
>>      'a': (func_a1, func_a2),
>>      'b': (func_b1, func_b2),
>>      'c': (func_c, )
>> }
>>
>>
>>
>> for f in switch.get(var, (default, )):
>>      f()
>>
>>
>> As a result of only some of the functions now requiring unique
>> arguments, I presume this needs to be migrated to a if/else statement?
>> Is there a way to maintain the switch style with the ability in this
>> scenario to cleanly pass args only to some functions?
>
>
> The dict-as-switch-statement pattern only works when the functions all
> take the same argument(s). Otherwise, you need to check which function
> you are calling, and provide the right arguments.

If, for instance, the functions take either 0 or 1 arg and the 1 arg is 
always the same, an alternative to the other suggestions is to look at 
the signature in an if statement.  In 3.3 this is relatively ease, as 
inspect.signature(func) returns a signature object. There are more 
complicated paths in earlier versions.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Switch statement "Joseph L. Casale" <jcasale@activenetwerx.com> - 2013-03-10 14:16 +0000
  Re: Switch statement Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-10 15:18 +0000
    RE: Switch statement "Joseph L. Casale" <jcasale@activenetwerx.com> - 2013-03-10 17:51 +0000
    Re: Switch statement Terry Reedy <tjreedy@udel.edu> - 2013-03-10 18:02 -0400

csiph-web