Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.albasani.net!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'table.': 0.07; 'pavel': 0.09; 'references.': 0.09; 'try:': 0.09; 'translate': 0.10; 'python': 0.11; 'def': 0.12; 'translation': 0.12; "wouldn't": 0.14; '"got': 0.16; 'article:': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'involved?': 0.16; 'subject:tasks': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'case.': 0.24; 'java': 0.24; 'switch': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'statement': 0.30; 'about.': 0.31; 'bunch': 0.31; 'keyerror:': 0.31; 'table,': 0.31; 'figure': 0.32; 'table': 0.34; "can't": 0.35; 'except': 0.35; 'but': 0.35; 'thanks': 0.36; 'message-id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'first': 0.61; 'talking': 0.65; 'invalid': 0.68; 'article': 0.77 X-Virus-Scanned: amavisd-new at torriefamily.org Date: Wed, 27 Nov 2013 11:15:16 -0700 From: Michael Torrie User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130105 Thunderbird/10.0.12 MIME-Version: 1.0 To: python-list@python.org Subject: Re: python for everyday tasks References: <5737051f-26d4-4771-b4a0-d41062f1a4ef@googlegroups.com> <52900c74$0$29993$c3e8da3$5496439d@news.astraweb.com> <27721690.YBritT5ChT@melforce> In-Reply-To: <27721690.YBritT5ChT@melforce> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385576149 news.xs4all.nl 15872 [2001:888:2000:d::a6]:33351 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60628 On 11/27/2013 11:05 AM, Pavel Volkov wrote: > Thanks for all those references. > There's this statement in the first article: > > "Got a switch statement? The Python translation is a hash table, not a bunch > of if-then statments. Got a bunch of if-then's that wouldn't be a switch > statement in Java because strings are involved? It's still a hash table. " > > I can't figure out how would you translate a switch statement into hash table > in general case. The general case is an if/elif ladder. But consider: def func1(): pass def func2(): pass def func3(): pass dispatch = { 0: func1, 1: func2, 2: func3, } # do some calc result = somecalc() try: dispatch[result]() except KeyError: # invalid result That's what the article is talking about.