On Fri, 28 Feb 2014 20:53:15 +0200, Marko Rauhamaa wrote: > "Mark H. Harris" <harrismh...@gmail.com>: > >> I think the real issue is about the syntax... because of python's >> unique indent strategy going back to ABC, a pythonized switch statement >> would play havoc with the many text parsers out there used for >> development (TestWrangler, and many others). > > I also took a look at the proposals. I don't think it's the editor > issue. The variant I proposed most recently: > > with self.state from Connection.State: > if CONNECTING or CONNECTED: > ... > elif DISONNECTING: > ... > else: > ... > > would be handled gracefully by all sane python editors, I believe. > > The main problem is that it can't be optimized effectively without > bringing in an element of preprocessing.
You can't have it both ways: you cannot claim that switch or case is more readable than a chain of if...elif, and then propose a syntax which is effectively a chain of if...elif while still claiming it is an improvement. It's not. All you've done is introduce an element of implicit magic to the syntax. >> a switch statement is just more readable to human beings that a dict >> dispatch table, or a long if elif chain... and one of the main points >> of python (going all the way back to ABC) was to make very highly >> readable code. > > A dict dispatch table is just awful. At least have the decency of > creating inner classes. Why (how?) would you want to use *multiple* classes for a single switch? Dict dispatch tables are elegant, attractive and efficient if you are using pre-existing functions or functions you can create using lambda: dispatch = { BLUE: handle_blue, RED: lambda arg: process(arg, this, that), ... } dispatch[key](something) It gets unwieldy if the functions don't already exist and cannot be embedded as a single expression. In that case, there are two simple approaches for when your switch cases are strings: - move the functions into a class, as self-less methods, then use the class __dict__ as the dispatch table; - or move the functions into an external module, and use the module __dict__ as the dispatch table. Even when you're not switching on strings, with the aid of a simple helper function, it's trivial: dispatch = {} def case(*args): def decorator(func): for a in arg: dispatch[a] = func return func return decorator class Dispatch: @case(BLUE) def handle_blue(something): ... @case(RED): def handle_red(something): ... dispatch[BLUE](something) There are all sorts of simple and neat ways to handle switches in Python. That's why the pressure on the language to grow special syntax is quite low. It simply isn't important enough. -- Steven -- https://mail.python.org/mailman/listinfo/python-list