On Sat, 01 Mar 2014 02:03:51 +0200, Marko Rauhamaa wrote: > Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>: > >> 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. > > You may be right that Python has too many practical problems for a truly > fitting switch syntax.
I didn't make that claim. My claim is that your suggested syntax is no real improvement over existing syntax. >> Why (how?) would you want to use *multiple* classes for a single >> switch? > > Well, I was just given a whole link on the very topic: > > <URL: http://c2.com/cgi/wiki?SwitchStatementsSmell> No no no, you've deleted the context. I'm not talking about making a switch statement unnecessary by using polymorphism. I was responding to your claim that instead of using a dict, one should: [quote] At least have the decency of creating inner classes. [end quote] I don't understand this. That's why I asked why and how. I can only imagine you mean something like this: class Outer: class InnerOne: ... class InnerTwo: ... class InnerThree: ... but I don't see how you get from that to the functionality of a dispatch table, or what you're supposed to do with the nested classes. > Here's a shorter one: > > <URL: http://en.wikipedia.org/wiki/State_pattern> That's not a way of implementing switching. That's a way of *avoiding* switching. Despite the name, it has little to do with state machines. > Anyway, while implementing states as singleton inner class instances is > elegant and highly readable, O_o That's terribly, terribly inelegant. Why not use the class objects themselves, instead of instantiating them? Even then, you haven't escaped the use of a dict dispatch table, or a chain of if...elif. You've just defined the symbols you use. > the technique suffers in one significant > respect: it can be very slow. Particularly if you program Java in Python. > Often a state machine has numerous states > but the typical execution path only visits a few of them. However, the > pattern calls for declaring classes (or instances) for all states in the > state machine constructor. Switch statements make it possible to avoid > this creation overhead. How do you avoid creating the classes or instances? If they haven't been created, how can the compiler reference them? >> Dict dispatch tables are elegant, attractive and efficient if you are >> using pre-existing functions or functions you can create using lambda: > > I beg to differ. The dict dispatch tables are very hard to read. The > fully blown-out if-else chain beats it in elegance hands down. Only if you lay out the dict badly. Poorly written code is ugly and hard to read no matter what you do. -- Steven -- https://mail.python.org/mailman/listinfo/python-list