RE: Switch statement

2013-03-10 Thread Joseph L. Casale
> Or could you do something like: > > arguments_to_pass = [list of some sort] > switch.get(var, default)(*arguments_to_pass) Stevens lambda suggestion was most appropriate. Within the switch, there are functions called with none, or some variation of arguments. It was not easy to pass them in afte

Re: Switch statement

2013-03-10 Thread Terry Reedy
On 3/10/2013 11:18 AM, Steven D'Aprano wrote: On Sun, 10 Mar 2013 14:16:27 +, Joseph L. Casale wrote: I have a switch statement composed using a dict: switch = { 'a': func_a, 'b': func_b, 'c': func_c } switch.get(var, default)() As

Re: Switch statement

2013-03-10 Thread Nicholas Cole
On Sun, Mar 10, 2013 at 8:42 PM, Mitya Sirenef wrote: > On 03/10/2013 10:16 AM, Joseph L. Casale wrote: > >> I have a switch statement composed using a dict: >> > > > > > > switch = { > > 'a': func_a, > > 'b': func_b, > >

Re: Switch statement

2013-03-10 Thread Mitya Sirenef
On 03/10/2013 10:16 AM, Joseph L. Casale wrote: I have a switch statement composed using a dict: > > > switch = { > 'a': func_a, > 'b': func_b, > 'c': func_c > } > switch.get(var, default)() > > > As a result of multiple functions

RE: Switch statement

2013-03-10 Thread Joseph L. Casale
> switch = {  > 'A': functools.partial(spam, a), > 'B': lambda b, c=c: ham(b, c), > 'C': eggs, > } >  > switch[letter](b) That's cool, never even thought to use lambdas. > functools.partial isn't always applicable, but when it is, you should > prefer it over lambda since it will

Re: Switch statement

2013-03-10 Thread Steven D'Aprano
On Sun, 10 Mar 2013 14:16:27 +, Joseph L. Casale wrote: > I have a switch statement composed using a dict: > > > switch = { >     'a': func_a, >     'b': func_b, >     'c': func_c > } > switch.get(var, default)() > >

Switch statement

2013-03-10 Thread Joseph L. Casale
I have a switch statement composed using a dict: switch = {     'a': func_a,     'b': func_b,     'c': func_c } switch.get(var, default)() As a result of multiple functions per choice, it migrated to: switch = {     'a': (func_a1, func_a2),     &

Re: Best way to make a number of tests against an object('s attributes) with absence of switch statement?

2008-06-16 Thread Bruno Desthuilliers
do is loop through 'pets', and test each object. Some of the tests I'd like to perform are: Is the size 'small' and species not 'dog'? Is the species 'cat' and name 'fluffy'? Is the species not 'dog' or 'cat'? In PHP I'

Re: Best way to make a number of tests against an object('s attributes) with absence of switch statement?

2008-06-13 Thread John Machin
': 'dog', 'size': 'large'} I'd suggest that you have a Pet class, instead of using dicts. > What I'd like to do is loop through 'pets', and test each object. Some > of the tests I'd like to perform are: > > Is the size 's

Best way to make a number of tests against an object('s attributes) with absence of switch statement?

2008-06-13 Thread Phillip B Oldham
s', and test each object. Some of the tests I'd like to perform are: Is the size 'small' and species not 'dog'? Is the species 'cat' and name 'fluffy'? Is the species not 'dog' or 'cat'? In PHP I'd use a switch statement

Re: case/switch statement?

2005-06-23 Thread Roy Smith
"NickC" <[EMAIL PROTECTED]> wrote: > The thing I love most about Python is the fact that callables can be > slung around at run-time, just like any other object. Yup. A while ago, I was doing a lot of file parsing with state machines. Each state was a function. The main loop of the state machi

Re: case/switch statement?

2005-06-23 Thread NickC
l of the form above, or are > eliminated entirely--so I don't miss a switch/case statement in > Python. I find the lack of a switch statement encourages me to write data-driven programs (which is a god-send when I have to enhance them later to deal with additional cases). The thing I l

Re: case/switch statement?

2005-06-22 Thread Skip Montanaro
Martin> The namespace issue alluded to doesn't exist when using the very Martin> similar approach suggested earlier in this thread by Andrew Martin> Durdin Sure, but I don't think I want to pay the cost of the exec statement. if/else would probably be quicker. -- shown again below i

Re: case/switch statement?

2005-06-22 Thread Martin Miller
Skip Montanaro wrote: > Terry> Yeah, and I find this even more so: > > Terry> case = { > Terry> 5: do_this, > Terry> 6: do_that, > Terry> } > Terry> case.get(x, do_default)() > > Terry> Which is looking pretty close to a case statement, anyway. > > S

Re: case/switch statement?

2005-06-19 Thread Andrew Durdin
On 6/18/05, D H <[EMAIL PROTECTED]> wrote: > I would hardly call using a > dictionary as a switch statement, the "equivalent". The fact that > people use a dictionary as a conditional is a python wart. Not at all. A case statement is nothing more than a literal mapping

Re: case/switch statement?

2005-06-19 Thread D H
Peter Hansen wrote: > D H wrote: > >> Peter Hansen wrote: > > [some stuff Doug didn't like] > Actually, this is what you snipped, stuff you didn't like, because the very mention of "boo" causes you such fits: > Since you and Steve Holden agree that a case statement is useful, why > don't yo

Re: case/switch statement?

2005-06-18 Thread Peter Hansen
D H wrote: > Peter Hansen wrote: [some stuff Doug didn't like] > I don't think you could have misread my simple suggestion to you any > more completely than you did. Sorry, I'll try harder next time. -Peter -- http://mail.python.org/mailman/listinfo/python-list

Re: case/switch statement?

2005-06-17 Thread D H
; > 1. You forgot my previous comment that "in current Python the equivalent > approach is, of course, a dictionary of some kind, though it's arguable > whether this is as clean in many cases as a case statement would be." I didn't forget that. I never read it, and

Re: case/switch statement?

2005-06-14 Thread Peter Hansen
D H wrote: > Peter Hansen wrote: >> With a case statement, on the other hand, you *know* that it must be >> just simple conditionals (a series of x == some_constant tests), so >> you don't need to look at all the cases, just the one that interests you. > > Since you and Steve Holden agree that a

Re: case/switch statement?

2005-06-13 Thread D H
Peter Hansen wrote: > With a case statement, on the other hand, you *know* that it must be > just simple conditionals (a series of x == some_constant tests), so you > don't need to look at all the cases, just the one that interests you. Since you and Steve Holden agree that a case statement is u

Re: case/switch statement?

2005-06-13 Thread Philippe C. Martin
Any speed issue ? [EMAIL PROTECTED] wrote: > Philippe C. Martin wrote: >> Leif K-Brooks wrote: >> >> > Joe Stevenson wrote: >> >> I skimmed through the docs for Python, and I did not find anything >> >> like >> >> a case or switch st

Re: case/switch statement?

2005-06-13 Thread Skip Montanaro
Terry> Yeah, and I find this even more so: Terry> case = { Terry> 5: do_this, Terry> 6: do_that, Terry> } Terry> case.get(x, do_default)() Terry> Which is looking pretty close to a case statement, anyway. Sure, modulo namespace issues. Skip

Re: case/switch statement?

2005-06-13 Thread Terry Hancock
On Sunday 12 June 2005 07:33 am, Dan Sommers wrote: > > There is no case statement in Python. If you don't care about > > readability, one alternative is to use a dictionary: > > > case = {5: do_this, 6: do_that} > > case.get(x, do_something_else)() > > I find this very readable. YMMV. Yeah, an

Re: case/switch statement?

2005-06-13 Thread Steve Holden
Peter Hansen wrote: > Chinook wrote: > >>On Sun, 12 Jun 2005 17:19:06 -0400, Peter Hansen wrote: >> >>>Case statements are actually more suitable in many cases even when >>>performance is not a goal for reasons of *readability*. >>> >>>When reading an if statement, you have to scan down through e

Re: case/switch statement?

2005-06-12 Thread Peter Hansen
Chinook wrote: > On Sun, 12 Jun 2005 17:19:06 -0400, Peter Hansen wrote: >>Case statements are actually more suitable in many cases even when >>performance is not a goal for reasons of *readability*. >> >>When reading an if statement, you have to scan down through effective >>each statement attem

Re: case/switch statement?

2005-06-12 Thread Raymond Hettinger
[Joe Stevenson] > > 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. [deelan] > topic already beated to death It's not dead yet. It has excellent use c

Re: case/switch statement?

2005-06-12 Thread Chinook
On Sun, 12 Jun 2005 17:19:06 -0400, Peter Hansen wrote (in article <[EMAIL PROTECTED]>): > Steven D'Aprano wrote: >> On Sat, 11 Jun 2005 19:47:58 -0500, Skip Montanaro wrote: >>> If the case values are constants known to the compiler, it can generate >>> O(1) >>> code to take the correct branch.

Re: case/switch statement?

2005-06-12 Thread Peter Hansen
Steven D'Aprano wrote: > On Sat, 11 Jun 2005 19:47:58 -0500, Skip Montanaro wrote: >>If the case values are constants known to the compiler, it can generate O(1) >>code to take the correct branch. >>It is precisely this behavior that is desired in many situations. > > Agreed. I certainly don't o

Re: case/switch statement?

2005-06-12 Thread Steven D'Aprano
On Sun, 12 Jun 2005 08:33:32 -0400, Dan Sommers wrote: >> I've never understood why something like: > >> if x = 5: >> do_this >> elif x = 6: >> do_that >> else: >> do_something_else > >> is supposed to be "bad", but > >> case of: >> x = 5: >> do_this >> x = 6: >>

Re: case/switch statement?

2005-06-12 Thread Dan Sommers
On Sun, 12 Jun 2005 10:35:42 +1000, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > I don't relish the idea of especially long case statements. > I've never understood why something like: > if x = 5: > do_this > elif x = 6: > do_that > else: > do_something_else > is supposed to be "bad

Re: case/switch statement?

2005-06-12 Thread invalidemail
Philippe C. Martin wrote: > Leif K-Brooks wrote: > > > Joe Stevenson wrote: > >> 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 plea

Re: case/switch statement?

2005-06-12 Thread Steven D'Aprano
On Sat, 11 Jun 2005 19:47:58 -0500, Skip Montanaro wrote: > If the case values are constants known to the compiler, it can generate O(1) > code to take the correct branch. (In fact, that could be done by the > compiler for if statements such as in your example today. It just isn't.) > It is prec

Re: case/switch statement?

2005-06-11 Thread Terry Hancock
On Saturday 11 June 2005 07:35 pm, Steven D'Aprano wrote: > On Sat, 11 Jun 2005 16:15:42 +, Joe Stevenson wrote: > > 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 > &g

Re: case/switch statement?

2005-06-11 Thread Skip Montanaro
Steven> I've never understood why something like: Steven> if x = 5: Steven> do_this Steven> elif x = 6: Steven> do_that Steven> else: Steven> do_something_else Steven> is supposed to be "bad", but Steven> case of: Steven> x = 5: Steven

Re: case/switch statement?

2005-06-11 Thread Steven D'Aprano
On Sat, 11 Jun 2005 16:15:42 +, Joe Stevenson wrote: > 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 docum

Re: case/switch statement?

2005-06-11 Thread Philippe C. Martin
I _love_ Python! Leif K-Brooks wrote: > Joe Stevenson wrote: >> 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 do

Re: case/switch statement?

2005-06-11 Thread Leif K-Brooks
Joe Stevenson wrote: > 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 th

Re: case/switch statement?

2005-06-11 Thread deelan
Joe Stevenson wrote: > 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. strictly speaking, python does not have a switch stamenent. "Why isn'

case/switch statement?

2005-06-11 Thread Joe Stevenson
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

Re: Simple, elegant, pythonic solution for switch statement

2005-04-26 Thread bruno modulix
python_only wrote: Check it out! Readable switch construction without lambdas or dictionaries: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410692 Not sure I'll have a need for it, but, yes, nice job !-) -- bruno desthuilliers python -c "print '@'.join(['.'.join([w[::-1] for w in p.split

Simple, elegant, pythonic solution for switch statement

2005-04-26 Thread python_only
Check it out! Readable switch construction without lambdas or dictionaries: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410692 -- http://mail.python.org/mailman/listinfo/python-list

Re: Switch statement (was: Lambda going out of fashion)

2005-01-03 Thread TZOTZIOY
On Thu, 23 Dec 2004 19:57:27 GMT, rumours say that rzed <[EMAIL PROTECTED]> might have written: [Why did PEP 275 stall?] >It seems to me that it was regarded as misguidod. QOTPE +1 (PE=Python Era) Oncoming Python book: "Hitchhiker's Guido to the Python Language" -- TZOTZIOY, I speak England v

Re: Switch statement (was: Lambda going out of fashion)

2004-12-23 Thread rzed
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 poten

Switch statement (was: Lambda going out of fashion)

2004-12-23 Thread Skip Montanaro
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(), lambda and x.blat() or x.blah()) to perform what might only be a small handful of inline statem