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


Groups > comp.lang.python > #104993

Re: Case Statements

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Newsgroups comp.lang.python
Subject Re: Case Statements
Date Wed, 16 Mar 2016 08:47:08 +0000
Lines 111
Message-ID <mailman.183.1458118080.12893.python-list@python.org> (permalink)
References <f30500db-19cb-4de8-81f1-6eb77c28b3b4@googlegroups.com> <30502a2e-0bad-4b0f-a1e8-a2b40b0d7ab9@googlegroups.com> <mailman.181.1458102409.12893.python-list@python.org> <ncb4e0$op1$1@dont-email.me>
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 <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; '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 <ncb4e0$op1$1@dont-email.me>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
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>
Xref csiph.com comp.lang.python:104993

Show key headers only | View raw


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 "<stdin>", line 1, in <module>
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

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


Thread

Case Statements jj0gen0info@gmail.com - 2016-03-15 13:46 -0700
  Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-15 23:11 +0000
  Re: Case Statements jj0gen0info@gmail.com - 2016-03-15 16:47 -0700
    Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-15 23:58 +0000
    Re: Case Statements BartC <bc@freeuk.com> - 2016-03-16 00:51 +0000
      Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 01:05 +0000
  Re: Case Statements jj0gen0info@gmail.com - 2016-03-15 18:55 -0700
    Re: Case Statements "Mario R. Osorio" <nimbiotics@gmail.com> - 2016-03-15 21:06 -0700
      Re: Case Statements BartC <bc@freeuk.com> - 2016-03-16 10:34 +0000
        Re: Case Statements Steven D'Aprano <steve@pearwood.info> - 2016-03-16 23:56 +1100
    Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 04:26 +0000
      Re: Case Statements Christian Gollwitzer <auriocus@gmx.de> - 2016-03-16 09:13 +0100
        Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 08:47 +0000
        Re: Case Statements Marko Rauhamaa <marko@pacujo.net> - 2016-03-16 11:16 +0200
        Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-16 10:35 +0100
        Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 09:51 +0000
          Re: Case Statements BartC <bc@freeuk.com> - 2016-03-16 19:41 +0000
            Re: Case Statements BartC <bc@freeuk.com> - 2016-03-16 22:30 +0000
        Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-16 11:52 +0100
        Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 11:07 +0000
          Re: Case Statements BartC <bc@freeuk.com> - 2016-03-16 11:16 +0000
            Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 11:33 +0000
            Re: Case Statements Marko Rauhamaa <marko@pacujo.net> - 2016-03-16 14:21 +0200
              Re: Case Statements BartC <bc@freeuk.com> - 2016-03-16 12:53 +0000
                Re: Case Statements Marko Rauhamaa <marko@pacujo.net> - 2016-03-16 16:31 +0200
                Re: Case Statements BartC <bc@freeuk.com> - 2016-03-16 15:00 +0000
                Re: Case Statements Marko Rauhamaa <marko@pacujo.net> - 2016-03-16 19:07 +0200
              Re: Case Statements Rustom Mody <rustompmody@gmail.com> - 2016-03-20 01:01 -0700
                Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-20 08:29 +0000
        Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-16 14:38 +0100
        Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 14:02 +0000
        Re: Case Statements Marko Rauhamaa <marko@pacujo.net> - 2016-03-16 21:27 +0200
          Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-17 09:22 +0100
            Re: Case Statements Marko Rauhamaa <marko@pacujo.net> - 2016-03-17 10:57 +0200
              Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-17 10:12 +0100
        Re: Case Statements Steven D'Aprano <steve@pearwood.info> - 2016-03-17 11:19 +1100
          Re: Case Statements Steven D'Aprano <steve@pearwood.info> - 2016-03-17 12:54 +1100
            Re: Case Statements Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-03-17 18:45 +1300
              Re: Case Statements Chris Angelico <rosuav@gmail.com> - 2016-03-17 17:30 +1100
                Re: Case Statements Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2016-03-18 18:59 +1300
              Re: Case Statements Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2016-03-17 17:29 +1100
                Re: Case Statements Chris Angelico <rosuav@gmail.com> - 2016-03-17 17:48 +1100
                Re: Case Statements Steven D'Aprano <steve@pearwood.info> - 2016-03-17 21:53 +1100
                Re: Case Statements Chris Angelico <rosuav@gmail.com> - 2016-03-17 22:49 +1100
            Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-17 10:23 +0100
            Re: Case Statements Chris Angelico <rosuav@gmail.com> - 2016-03-17 20:55 +1100
          Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-17 09:36 +0100
        Re: Case Statements Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-03-17 09:38 +0100
  Re: Case Statements l0r0m0a0i0l@gmail.com - 2016-03-16 06:15 -0700
    Re: Case Statements Steven D'Aprano <steve@pearwood.info> - 2016-03-17 00:27 +1100
      Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 14:00 +0000
    Re: Case Statements Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-16 13:59 +0000
  Re: Case Statements l0r0m0a0i0l@gmail.com - 2016-03-16 07:21 -0700
    Re: Case Statements Marko Rauhamaa <marko@pacujo.net> - 2016-03-16 16:33 +0200

csiph-web