[Python-ideas] Yet Another Switch-Case Syntax Proposal

Bruce Leban bruce at leapyear.org
Sun Apr 20 04:17:23 CEST 2014


On Sat, Apr 19, 2014 at 10:42 AM, Lucas Malor <7vsfeu4pxg at snkmail.com>wrote:

> On 19 April 2014 08:52, Bruce Leban bruce-at-leapyear.org |
> python-ideas-at-python..org <http://python-ideas-at-python.org>| <
> vajg1g2cqt at sneakemail.com> wrote:
>
>> Here's a simple class that provides case like syntax. How would modifying
>> the language be better than this?
>>
>
> I don't like the additional unneeded Case indentation. That's why I'm
> proposing a syntax with switch on the same line with the first case.
>

I found that the ugliest part of your proposal since it hides the first
case and at the same time makes it appear special when it isn't.

And merely saving indentation is not IMHO a sufficient reason to add a new
feature to the language especially since you're not actually saving any.
The indentation isn't required with my little Case class. The following
works just as well but lacks the visible scoping that the with statement
provides (of course the scope of the case variable leaks in either version,
but I could modify my class to fail if invoked after __exit__ or __del__).

for i in range(5):
    print(i, end=' => ')
    case = Case(i)
    if case(1):
        print('one')
    elif case((2,3)):

        print('tuple(two, three)')

    elif case(2, 3):
        print('two or three')
    elif case > 3:
        print('more than three')

    else:

        print('unmatched')



--- Bruce
Learn how hackers think: http://j.mp/gruyere-security
https://www.linkedin.com/in/bruceleban
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140419/d7f9def2/attachment-0001.html>


More information about the Python-ideas mailing list