[Python-Dev] switch-based programming in Python (original) (raw)

Thomas Wouters thomas@xs4all.net
Thu, 8 Nov 2001 17:51:48 +0100


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 ;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.

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.

[ 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'.

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 :)

-- Thomas Wouters <thomas@xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!