[Python-Dev] switch-based programming in Python

M.-A. Lemburg mal@lemburg.com
Fri, 09 Nov 2001 09:30:26 +0100


Thomas Wouters wrote:
> 
> On Thu, Nov 08, 2001 at 04:45:07PM +0100, M.-A. Lemburg wrote:
> 
> > BTW, how did you get XS4ALL into funding the www.python.org traffic, if
> > they don't heavily depend on Python ?
> 
> That's a complicated story. I'll be happy to explain it over a beer at IPC10
> (if we both make it there 

I probably won't :-(

> ;P) but the short version is that XS4ALL is not an
> ordinary company, we use a lot of opensource software, and my boss suggested
> it. The traffic is peanuts, by the way, I think the more costly part is the
> rackspace in our system room.

Interesting; I would have thought that the traffic would at least
cost as much as the rack space (in Germany we pay EUR 6 / GB traffic).
 
> > I think that Skip's proposal would go a long way (sketching here
> > a bit):
> 
> > It should be possible for the compiler to detect an if-elif-else
> > construct which has the following signature:
> 
> > if x == 'first':...
> > elif x == 'second':...
> > else:...
> 
> [..]
> 
> > At runtime, the interpreter would check x for being one of the
> > well-known immutable types (strings, unicode, numbers) and
> > use the hash table for finding the right opcode snippet.
> 
> Hmm... I don't think this will have as much impact as you think. But testing
> it like Martin suggested would be a good idea, and the compiler/interpreter
> is a fun thing to play and experiment with.

Well, for that application space I'm after this would most probably
make a difference (you typically have >10 cases in the if-elif-else).
I think I'll make this a holiday experiment and then see what the
real gain is.
 
> [ About my switch proposal ]
> > I think you missed some indents in your example. I added them again,
> > removing the parens around x and tweaked the formatting a bit (also
> > note the addition of a few breaks).
> 
> Actually, no, all but the parentheses were intentional. I don't like needing
> the break (hence my comments about fallthrough) and I think the switch, case
> and else should all be indented to the same level, just like 'if/elif/else'.

Ok.
 
> >    def whatis(x):
> >        switch x:
> >            case 'one':
> >                print '1'
> >                break
> 
> > Turns out that this look very Pythonic :-)
> 
> I like my version better, with the exception of the parentheses around 'x'
> in 'switch(x):' :)
> 
> > Sure smells like PEP-time :-)
> 
> Aye, but lets do it while Guido is still on paternity leave so we at least
> get to finish the proposal before it's -1'ed :)

Let's make it a two part PEP: one part should focus on the
optimization idea and the other one on a new syntax. That'll
turn the -1 into a -0.5 which gets rounded towards 0 and then
makes a difference ;)

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Consulting & Company:                           http://www.egenix.com/
Python Software:                        http://www.lemburg.com/python/