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?? 14 2617
"Rudi Hansen" <rs************ **@pobox.dk> writes: 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]
--
Marius Bernklev
Rudi Hansen <rs************ **@pobox.dk> wrote: 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.writ e('var=1\n'),
2: lambda: sys.stdout.writ e('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
In article <CR************ ********@news00 0.worldonline.d k>,
Rudi Hansen <rs************ **@pobox.dk> wrote: 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"?
--
\S -- si***@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
In article <1Z*******@news .chiark.greenen d.org.uk>,
Sion Arrowsmith <si***@chiark.g reenend.org.uk> wrote: In article <CR************ ********@news00 0.worldonline.d k>, Rudi Hansen <rs************ **@pobox.dk> wrote: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.
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.
>>>>> "Roy" == Roy Smith <ro*@panix.co m> writes:
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.
In article <ro************ ***********@rea der1.panix.com> ,
Roy Smith <ro*@panix.co m> wrote:
Isaac To <ik****@netscap e.net> wrote: 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.
Don't blame c++. This is a relic of 'C'. http://catb.org/~esr/jargon/html/D/Duffs-device.html
On Fri, 10 Sep 2004 11:16:12 -0400, Roy Smith <ro*@panix.co m> wrote: Isaac To <ik****@netscap e.net> wrote: 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.
-- http://mail.python.org/mailman/listinfo/python-list
Isaac To wrote: 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Christian Seberino |
last post by:
I know IPython is another interpreter for Python and was wondering
what people liked about it and if I should switch to it.
If it is so good then why is it not part of the standard Python
tarball?
Chris
|
by: Jeff Duffy |
last post by:
Hi all.
I've been wondering why python itself doesn't provide a switch to
check a file for valid syntax. I know that you can currently call
python -c "import py_compile; py_compile.compile(r'MyApp.py')"
but in order to manage this effectively you have to add a shell alias,
write a script, mess about with your editor, or what have you. This
becomes yet another small annoyance I'd like to get rid of. I'm also
|
by: Skip Montanaro |
last post by:
Stephen> {
Stephen> 'one': lambda x:x.blat(),
Stephen> 'two': lambda x:x.blah(),
Stephen> }.get(someValue, lambda x:0)(someOtherValue)
One thing to remember is that function calls in Python are pretty damn
expensive. If x.blat() or x.blah() are themselves only one or two lines of
code, you might find that your "switch" statement is better written as an
if/elif/else statement. You're making potentially three function calls
(get(),...
|
by: Joe Stevenson |
last post by:
Hi all,
I skimmed through the docs for Python, and I did not find anything like
a case or switch statement. I assume there is one and that I just
missed it. Can someone please point me to the appropriate document, or
post an example? I don't relish the idea especially long if-else
statements.
Joe
|
by: Mike and Jo |
last post by:
I've been converting some code to C++. I'm trying to use the Switch
function to compare a result. Is it possible to use switch to evaluate
'>0', '<0', 0?
Example
switch (result)
{
case (>0):
case (<0):
| |
by: He Shiming |
last post by:
Hi,
I just wrote a function that has over 200 "cases" wrapped in a "switch"
statement. I'm wondering if there are performance issues in such
implementation. Do I need to optimize it some way?
In terms of generated machine code, how does hundreds of cases in a switch
differ from hundreds of if-elses? Do compilers or processors do any
optimization on such structured code? Do I need to worry about the
performance, usually?
|
by: Pan Xingzhi |
last post by:
Guys:
Hi there. Recently I'll have to write a quite interesting program
in Python on a Linux box. What I need is a function which allows the
user to 'switch' the audio output from <an audio
file>/<microphone>/<line in>.
I'm not quite familiar with Linux programming. I've checked some
python media frameworks but still need some light. Does anybody have
experience on this? Thanks in advance!
|
by: Sile |
last post by:
Hello,
I'm trying to get f2py working from the command line on windows XP. I
have mingw32 as my C complier (after some advice on a previous thread)
and Compaq Visual Fortran 6.5. Changing my C complier reduced my errors
but I'm still having trouble. I think I have all the correct paths set
but I'm not sure. F2PY gets further when I specifically tell it what my
compilers are as follows.................
C:\Program...
|
by: Phillip B Oldham |
last post by:
What would be the optimal/pythonic way to subject an object to a
number of tests (based on the object's attributes) and redirect
program flow?
Say I had the following:
pets = {'name': 'fluffy', 'species': 'cat', 'size': 'small'}
pets = {'name': 'bruno', 'species': 'snake', 'size': 'small'}
pets = {'name': 'rex', 'species': 'dog', 'size': 'large'}
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |