Is there no switch function in Python

R

Rudi Hansen

I dont seem to be able to find the switch statement in Python.

I would like to be able to do

switch(var)
case 1 :
print "var = 1"
case 2:
print "var = 2"

But it seems that i have to do.

if(var=1)
print "var =1"
elseif(var=2)
print "var=2"

Is ther no easier way??
 
M

Marius Bernklev

Rudi Hansen said:
I would like to be able to do

switch(var)
case 1 :
print "var = 1"
case 2:
print "var = 2"

But it seems that i have to do.

if(var=1)
print "var =1"
elseif(var=2)
print "var=2"

Is ther no easier way??

one way is using dicts:

swoosh = {1: "var = 1", 2: "var = 2"}
print swoosh[var]
 
A

Alex Martelli

Rudi Hansen said:
I dont seem to be able to find the switch statement in Python.

Right, there isn't one.
I would like to be able to do

switch(var)
case 1 :
print "var = 1"
case 2:
print "var = 2"

But it seems that i have to do.

if(var=1)
print "var =1"
elseif(var=2)
print "var=2"

Is ther no easier way??

several, starting with

if var in (1,2): print 'var = %s' % var

The most Pythonic idiom, when you have to do something substantial in
each branch of the switch, is a dictionary of callables -- in this toy
case it might be:

switch = {1: lambda: sys.stdout.write('var=1\n'),
2: lambda: sys.stdout.write('var=2\n'), }
switch.get(var, lambda: '')()

An if/elif tree is also fine, though the syntax is not as you think...:

if var == 1:
print 'var is one'
elif var == 2:
print 'var is two'



Alex
 
S

Sion Arrowsmith

I dont seem to be able to find the switch statement in Python.

I would like to be able to do

switch(var)
case 1 :
print "var = 1"
case 2:
print "var = 2"

5 lines, 55 characters including the print statements.

But it seems that i have to do.

if(var=1)
print "var =1"
elseif(var=2)
print "var=2"

4 lines, 52 characters (once corrected) including the print
statements (as written in the first example).
Is ther no easier way??

In what way do you want it to be "easier"?
 
R

Roy Smith

Sion Arrowsmith said:
5 lines, 55 characters including the print statements.

It's even longer if you include the required "break" statement at the
end of case 1 (assuming you're talking about a C-style switch statement
with fall-through cases).

That being said, there are times when I miss switch. For some kinds of
multi-branch logic, I think it expresses the meaning better than a
string of if's. But it's hardly necessary.

The real reason, IMHO, switch existed in C was because it would let the
compiler build very efficient jump tables for switches with many cases.
Imagine switching on the ascii value of a character and having a 128
different cases. The compiler could build a 128 entry jump table and
the switch statement would compile down to a single machine instruction.
 
I

Isaac To

Roy> The real reason, IMHO, switch existed in C was because it
Roy> would let the compiler build very efficient jump tables for
Roy> switches with many cases. Imagine switching on the ascii
Roy> value of a character and having a 128 different cases. The
Roy> compiler could build a 128 entry jump table and the switch
Roy> statement would compile down to a single machine instruction.

C switch is much more flexible than one would expect. It is not just
a cheap replacement of a sequence of if-then-else. E.g., in an
exercise of "The C++ Programming Language" of Bjarne Stroustrup, you
can see the following example code:

void send(int *to, int *from, int count) {
int n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while (--n > 0);
}
}

Note that this reduces the number of branch statements to execute
8-folds for any compiler but the smartest (as compared to the simplest
code "do { *to++ = *from++ } while (--count > 0);").

I'd instead guess that C has switch because at the times before C,
people code in assembly, and those tricks are popular. The design of
most programming language represents the norm of coding at that time.

Regards,
Isaac.
 
C

Cameron Laird

.
.
.
That being said, there are times when I miss switch. For some kinds of
multi-branch logic, I think it expresses the meaning better than a
string of if's. But it's hardly necessary.

The real reason, IMHO, switch existed in C was because it would let the
compiler build very efficient jump tables for switches with many cases.
Imagine switching on the ascii value of a character and having a 128
different cases. The compiler could build a 128 entry jump table and
the switch statement would compile down to a single machine instruction.

I probably miss switches as much as any Python coder. Having
appropriately introduced the concept of "jump table", though,
it's only right that we mention how often Python uses dictionaries
in just such ways.
 
R

Roy Smith

Isaac To said:
C switch is much more flexible than one would expect. It is not just
a cheap replacement of a sequence of if-then-else. E.g., in an
exercise of "The C++ Programming Language" of Bjarne Stroustrup, you
can see the following example code:

void send(int *to, int *from, int count) {
int n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while (--n > 0);
}
}

La-de-dah, Bjarne discovers loop unrolling. You don't need a switch
statement to unroll loops.

Hold on a second. Cases 1 through 7 jump into the middle of the do
loop!? Pardon me while I barf. I didn't even know that was legal.
That's the kind of code that gives C++ a bad name.
 
P

Peter Otten

Isaac said:
a cheap replacement of a sequence of if-then-else. E.g., in an
exercise of "The C++ Programming Language" of Bjarne Stroustrup, you
can see the following example code:

void send(int *to, int *from, int count) {
int n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while (--n > 0);
}
}

This is otherwise known a Duff's device:

http://catb.org/~esr/jargon/html/D/Duffs-device.html

Peter
 
B

Bengt Richter

Right, there isn't one.


several, starting with

if var in (1,2): print 'var = %s' % var

The most Pythonic idiom, when you have to do something substantial in
each branch of the switch, is a dictionary of callables -- in this toy
case it might be:

switch = {1: lambda: sys.stdout.write('var=1\n'),
2: lambda: sys.stdout.write('var=2\n'), }
switch.get(var, lambda: '')()

An if/elif tree is also fine, though the syntax is not as you think...:

if var == 1:
print 'var is one'
elif var == 2:
print 'var is two'
The trouble is that other than if/elif/else or some weird exception tricks,
you can't switch between statements that execute as if they were suites of if/elif/else -- i.e.,
in the local namespace. And UIAM current exec introduces special restrictions in a function.
So you can't get a constant-time "jump table" effect.

But IWT if we had named local code suites, we could exec them safely via a mapping. E.g.,

def foo(var): # following is not legal code ;-)
v1: print 'var is one'
v2: # any suite should be ok, not just one-liners
print 'var',
print 'is two'
switch = {1:v1, 2:v2}
exec switch.get(var, '')

It would also be nice if the local bindings of named local code suites were
(or became wrapped on access as) objects that were callable like functions
without parameters. Locally that could be optimized, but if e.g. foo returned
v2 to its caller, there would have to be a wrapper with a mutable closure
capturing the current name space IWT. This would also be interesting to
pass to another function, which then could effectively execute a code suite
in _its_ caller's space. This could lead to all kinds of wild things, _some_ of
which would probably be neat and useful ;-)

It would be nice to get the switch mapping built at def-time, which should be
feasible, since the v1,v2 etc, bindings could be available then. Not sure how
to spell it nicely though, except that I still like the notion of a general
way to specify def-time execution of statements or expression terms. E.g.,
using '..' as a syntactic marker,
switch ..= {1:v1, 2:v2}
could mean create the binding at def-time (necessarily evaluating the rhs then also).

I had previously proposed that
def foo():
x ..= 123
return x
be codewise equivalent to current
def foo(x=123):
return x
without having x appear in the argument list, but still having the same
call-time local-binding initialization effect (i.e., new binding to def-time rhs value).
I mention this because v1 and v2 in the example are not in the namespace that is normally
searched for default arg rhs expressions as in def foo(x=name): ..., so named local suites'
names would have to be included (and allowed to shadow names that might otherwise be found).

No one commented on my previous post on that, perhaps because I didn't split it out into
a separate post with appropriate title. So here I did it again ;-/ The main use would be
efficient local binding initialization without using the default-arg hack or using a factory
funtion to return a function with initialized bindings in a closure.

The other use would allow constant folding for terms in expressions, using ..(expr) as
as spelling for evaluating expr at def-time and using a constant binding to the result
at run-time. E.g.,

def circle_area(r): return ..(__import__('math').pi)*r*r

would generate byte code like what you can now do with Raymond Hettinger's global-constant
optimizing decorator by writing:

pi = __import__('math').pi # just to make it look like the expr above
@bind_constants()
def circle_area(r): return pi*r*r
del pi

(Very nice, Raymond)

Maybe Raymond will make a decorator to do local variable initialization, so we can write
@localinit('name', expr)
def foo():
name = name*2 # usable like default name in def foo(name=expr): ...
return name
;-)

Regards,
Bengt Richter
 
L

Leif K-Brooks

Bengt said:
But IWT if we had named local code suites, we could exec them safely via a mapping. E.g.,

def foo(var): # following is not legal code ;-)
v1: print 'var is one'
v2: # any suite should be ok, not just one-liners
print 'var',
print 'is two'
switch = {1:v1, 2:v2}
exec switch.get(var, '')

I'm probably missing something, but what's wrong with:

def foo(var):
def v1(): print 'var is one'
def v2():
print 'var',
print 'is two'
switch = {1:v1, 2:v2}
switch.get(var, lambda:None)()
 
L

Leif K-Brooks

Jaime said:
Don't blame c++. This is a relic of 'C'.

Kind of like saying, "Don't blame me for being a violent thug, I went to
ShootEm Elementary School." Sure, C++ inherited lots of questionable
features from C, but there's never been a law requiring all programming
languages to be extensions of C. The designers of C++ must have known
about C's faults; it's their own fault for choosing to use it in the
first place.
 
A

Alex Martelli

Leif K-Brooks said:
I'm probably missing something, but what's wrong with:

def foo(var):
def v1(): print 'var is one'
def v2():
print 'var',
print 'is two'
switch = {1:v1, 2:v2}
switch.get(var, lambda:None)()

Nothing wrong. However, if v1 and v2 needed to rebind some names within
foo, they couldn't according to def's semantics -- they could only
rebind local names of their own, or global names with a global
statement, but not local names in their outer function foo. I think the
right approach, if that feature is a must-have, might be to add the one
missing feature, not with a weird new syntax which forces one to choose
between taking arguments and rebinding outer functions' locals, but with
an optional scope indicator on variables. Just IMHO, anyway.


Alex
 
B

Bengt Richter

I'm probably missing something, but what's wrong with:

def foo(var):
def v1(): print 'var is one'
def v2():
print 'var',
print 'is two'
switch = {1:v1, 2:v2}
switch.get(var, lambda:None)()

Sorry, I should have shown an example that really depends on local name space rebinding,
which the OP's example doesn't, and which was my main point. E.g., try getting the effect of:

def foo(var): # following is not legal code ;-)
v1: print 'var is one'
var += 1
status = 'incremented'
v2: # any suite should be ok, not just one-liners
print 'var',
print 'is two'
var *= 2
status = 'doubled'
switch = {1:v1, 2:v2}
exec switch.get(var, '')
print 'var is now %r, and was %s.' % (var, status)
return var, status

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,675
Latest member
RollandKna

Latest Threads

Top