> 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
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
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,
> >
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
> 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
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)()
>
>
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),
&
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'
': '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
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
"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
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
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
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
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
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
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
;
> 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
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
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
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
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
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
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
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
[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
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.
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
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:
>>
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
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
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
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
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
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
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
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
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'
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
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
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
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
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
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
44 matches
Mail list logo