Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Mark Lawrence Newsgroups: comp.lang.python Subject: Re: Case Statements Date: Wed, 16 Mar 2016 08:47:08 +0000 Lines: 111 Message-ID: References: <30502a2e-0bad-4b0f-a1e8-a2b40b0d7ab9@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de HrXg24XnCcUKbFF/ywdRYw8ffDI/+4WTuNbsHORndaKw== Return-Path: 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; 'python,': 0.02; 'else:': 0.03; 'win32': 0.03; 'from:addr:yahoo.co.uk': 0.05; "'a'": 0.07; 'clause': 0.07; 'events.': 0.09; 'nameerror:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sane': 0.09; 'semantics': 0.09; 'statements': 0.09; 'switches': 0.09; 'python': 0.10; 'def': 0.13; 'api,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'scope.': 0.16; 'scripts.': 0.16; 'sense:': 0.16; 'simulate': 0.16; 'statements,': 0.16; 'switch.': 0.16; 'worst': 0.16; 'wrote:': 0.16; 'instance,': 0.18; 'pointer': 0.18; 'gui': 0.18; 'shell': 0.18; 'language': 0.19; '>>>': 0.20; 'windows': 0.20; 'otherwise,': 0.20; '"",': 0.22; 'lawrence': 0.22; 'parsing': 0.22; 'variables.': 0.22; 'appears': 0.23; 'decide': 0.23; 'defined': 0.23; 'bit': 0.23; 'dec': 0.23; 'tables': 0.23; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'discussion': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'command': 0.26; 'header:X-Complaints-To:1': 0.26; 'switch': 0.27; 'used,': 0.27; 'least': 0.27; 'back.': 0.27; 'function': 0.28; 'idea': 0.28; 'skip:( 20': 0.28; 'cases.': 0.29; 'for,': 0.29; 'hash': 0.29; 'repair': 0.29; 'print': 0.30; 'that.': 0.30; 'code': 0.30; 'e.g.': 0.30; "can't": 0.32; 'up.': 0.32; 'table': 0.32; 'language.': 0.32; 'statement': 0.32; 'traceback': 0.33; 'similar': 0.33; 'languages': 0.34; 'throughout': 0.34; 'file': 0.34; 'add': 0.34; 'gives': 0.35; 'skip:c 30': 0.35; 'something': 0.35; 'but': 0.36; 'possible': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'agree': 0.37; 'received:org': 0.37; 'list.': 0.37; 'difference': 0.38; 'christian': 0.38; 'mailing': 0.38; 'mean': 0.38; 'rather': 0.39; 'skip:x 10': 0.40; 'to:addr:python.org': 0.40; 'where': 0.40; 'mark': 0.40; 'default': 0.61; 'charset:windows-1252': 0.62; 'strange': 0.63; 'more': 0.63; 'different': 0.63; 'interest': 0.64; 'our': 0.64; 'between': 0.65; 'six': 0.65; 'statement,': 0.66; "they're": 0.66; 'incoming': 0.70; 'wish': 0.71; '3.5.1': 0.84; 'gollwitzer': 0.84; 'like:-': 0.84; 'posible': 0.84; 'pythonistas,': 0.84; 'simulation': 0.91; 'round,': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 80.234.129.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104993 On 16/03/2016 08:13, Christian Gollwitzer wrote: > Am 16.03.16 um 05:26 schrieb Mark Lawrence: >> So you would rather write something like:- >> >> switch (x): >> case COW: >> moo() >> break >> >> case DUCK: >> quack() >> break >> >> default IDUNNO: >> panic() >> >> than:- >> >> x.makeNoise() > > No sane person would do that. But just because you selected the worst > possible semantics (fallthrough/break) and the worst posible use case > (simulating polymorphism) doesn't mean that switches have no use. Code > like the above was used a lot in early GUI toolkits for C - Motif and > the Windows C API, for instance, use a gigantic switch to decide upon > incoming events. Now the strange statement in the switch discussion > makes sense: I've never said that switches have no use. As the powers to be have decreed that they're not going into Python, and I fully agree with that decision, I just wish people would shut up about them, but the issue keeps cropping up. > > "Typically, similar switch statements are scattered throughout a > program. If you add or remove a clause in one switch, you often have to > find and repair the others too." > > That happens indeed if one were to simulate polymorphism using switch > statements, but not for other cases. > > In Python, you need to go the other way round, you don't have a switch, > but you can simulate it via function tables or polymorphism. > The difference between a switch and its simulation via function pointer > tables etc. is the scope. In a true switch statement, the code blocks > access the same variables. You can't truly simulate an if statement in > Python, if it were missing: I've no idea at all what you mean by the above. > > >>> def hi(): > ... print "hi" > ... > >>> def yu(): > ... print "yu" > ... > >>> hi() if 1>2 else yu() > yu > >>> hi() if 2>1 else yu() > hi > > This gives you, in principal, an if-then-else statement back. But you > can't do something like > > if 1>2: > a=3 > else: > b=2 The above appears to have crept into Python when you weren't looking. C:\Users\Mark\Documents\MyPython>py -3 Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> if 1>2: ... a=2 ... else: ... b=3 ... >>> a Traceback (most recent call last): File "", line 1, in NameError: name 'a' is not defined >>> b 3 > > Same with switch. You can use a hash table etc. to simulate switches, > but only if the codeblocks are independent. Otherwise, if-elif chains > are the way to go. Command line parsing is a case where switch > statements are often used, e.g. in shell scripts. I've seen at least six different ways of simulating switches, so those people who want them, can have them. if-elif chains are not likely to kill any Python programmer. I have no interest what other languages use switch/case statements for, as we've on the PYTHON mailing list. > > Christian -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence