On 2/3/19 9:03 PM, Avi Gross wrote:
> The example I show above could in many cases be done as you describe
> but what are you gaining?
>
> I mean if I subtract the integer representation of a keyboard
> alphabetic letter (ASCII for the example) from letter 'a' or 'A' then
> A maps to 0 and B maps
list On
Behalf Of Dan Sommers
Sent: Sunday, February 3, 2019 8:29 PM
To: python-list@python.org
Subject: Re: Switch function
On 2/3/19 5:40 PM, Avi Gross wrote:
> Bottom line, does anyone bother using anything like this? It is
> actually a bunch of hidden IF statements matched in order bu
On 2/3/19 5:40 PM, Avi Gross wrote:
Bottom line, does anyone bother using anything like this? It is actually a
bunch of hidden IF statements matched in order but may meet many needs.
I sure don't. In the rare case that I might use a switch
statement in another language, I just use a series of
> 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 a result of multiple functions per choic
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,
> > 'c': func_c
> > }
> > switch.get(var, default)()
> >
> >
> > As a result of
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 per choice, it migrated to:
>
>
>
> switch = {
> 'a': (func_a1,
> 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)()
>
>
> As a result of multiple functions per choice, it migrated to:
>
>
>
In article ,
Tim Chase wrote:
>On 12/10/2009 09:22 PM, John Bokma wrote:
>>
>> Please don't delete attribution line(s), added:
>>
>> Asun Friere writes:
>
>I tend to prune them because a good newsreader will thread messages
>and put my reply in the context of the message to which I'm repl
In article ,
Steven D'Aprano wrote:
>On Wed, 09 Dec 2009 18:50:29 +, Nobody wrote:
>>
>> Compiled languages' switch statements typically require constant labels
>> as this enables various optimisations.
>
>Pascal, for example, can test against either single values, enumerated
>values, or a ra
On Dec 11, 1:38 am, Tim Chase wrote:
> It's clean if it were the solution to my problem
Picking out that line first, just to be clear about this.
You missed the disclaimer.
This was never meant to be a solution to your problem. It was
solution to the problem contained in the code you posted.
On 12/10/2009 09:22 PM, John Bokma wrote:
Tim Chase writes:
Please don't delete attribution line(s), added:
Asun Friere writes:
I tend to prune them because a good newsreader will thread
messages and put my reply in the context of the message to which
I'm replying. Both Thunderbird an
Bruno Desthuilliers:
> Well, obviously such business rules must by no mean be hardcoded. You
> really need a "rule engine", configurable by your domain experts thru a
> DSL that we'll design specially for you. The rule engine will generate
> an AbstractScoreFactory that will instanciate appropriat
Tim Chase writes:
Please don't delete attribution line(s), added:
Asun Friere writes:
>>
>> phone.update_from_record(record)
>>
>> This switch statement belongs to one guy. One guy who wants to know
>> how to do everything that needs to be done to Phones no matter who
>> asks
>
> T
Steven D'Aprano wrote:
On Wed, 09 Dec 2009 18:50:29 +, Nobody wrote:
On Tue, 08 Dec 2009 21:02:44 -0800, Kee Nethery wrote:
I string together a bunch of elif statements to simulate a switch
if foo == True:
blah
elif bar == True:
blah blah
elif bar == False:
blarg
On Thu, 10 Dec 2009 05:47:19 +, Steven D'Aprano wrote:
>>> I string together a bunch of elif statements to simulate a switch
>>>
>>> if foo == True:
>>> blah
>>> elif bar == True:
>>> blah blah
>>> elif bar == False:
>>> blarg
>>> elif
>>
>> This isn't what would normally be
Asun Friere wrote:
On Dec 10, 2:00 pm, Carl Banks wrote:
On Dec 9, 5:02 pm, Asun Friere wrote:
On Dec 9, 7:08 pm, Carl Banks wrote:
What if the object is a string you just read from a file?
How do you dispatch using polymorphism in that case?
This would be a pertinent question, w
Great example Tim. This is something that many of us must be dealing
with on a daily basis. The problem has enough details (bar one), to
allow an answer and not so detailed as to be confusing. And for me
it's a particularly good example, because your need accommodate
mulitple provider formats
On Dec 10, 3:34 am, Asun Friere wrote:
> On Dec 10, 2:00 pm, Carl Banks wrote:
[snip most of questionable, verly verbose reply]
> > You argued that a decent language OO should never
> > have a switch statement because polymorphic dispatch is the right way
> > to handle it in OO languages, which
On Dec 10, 6:57 am, Tim Chase wrote:
> Carl Banks wrote:
> > What if the object is a string you just read from a file?
>
> > How do you dispatch using polymorphism in that case?
>
> This is where I most miss a switch/case statement in Python...I
> do lots of text-file processing (cellular provider
On Dec 10, 2:00 pm, Carl Banks wrote:
> On Dec 9, 5:02 pm, Asun Friere wrote:
>
> > On Dec 9, 7:08 pm, Carl Banks wrote:
>
> > > What if the object is a string you just read from a file?
>
> > > How do you dispatch using polymorphism in that case?
>
> > This would be a pertinent question, were I
On Wed, 09 Dec 2009 18:50:29 +, Nobody wrote:
> On Tue, 08 Dec 2009 21:02:44 -0800, Kee Nethery wrote:
>
>> I string together a bunch of elif statements to simulate a switch
>>
>> if foo == True:
>> blah
>> elif bar == True:
>> blah blah
>> elif bar == False:
>> blarg
>> elif
On Dec 9, 5:02 pm, Asun Friere wrote:
> On Dec 9, 7:08 pm, Carl Banks wrote:
>
> > What if the object is a string you just read from a file?
>
> > How do you dispatch using polymorphism in that case?
>
> This would be a pertinent question, were I advocating that _all_
> switch statements should,
On Dec 9, 7:08 pm, Carl Banks wrote:
> What if the object is a string you just read from a file?
>
> How do you dispatch using polymorphism in that case?
This would be a pertinent question, were I advocating that _all_
switch statements should, or even can, be replaced with "dispatch
using polymo
On Dec 9, 5:39 pm, Steven D'Aprano
wrote:
> On Tue, 08 Dec 2009 21:36:23 -0800, Asun Friere wrote:
> > On Dec 9, 4:02 pm, Kee Nethery wrote:
> >> I string together a bunch of elif statements to simulate a switch
>
> >> if foo == True:
> >> blah
> >> elif bar == True:
> >> blah bl
MRAB wrote:
Tim Chase wrote:
switch row['recordtype']:
case '01':
phone.international += Decimal(row['internationalcost'])
// optionally a "break" here depending on
// C/C++/Java/PHP syntax vs. Pascal syntax which
// doesn't have fall-through
case '02':
ph
Tim Chase wrote:
Carl Banks wrote:
What if the object is a string you just read from a file?
How do you dispatch using polymorphism in that case?
[snip]
which would nicely change into something like
switch row['recordtype']:
case '01':
phone.international += Decimal(row['inter
Carl Banks wrote:
What if the object is a string you just read from a file?
How do you dispatch using polymorphism in that case?
This is where I most miss a switch/case statement in Python...I
do lots of text-file processing (cellular provider data), so I
have lots of code (for each provider
On Tue, 08 Dec 2009 21:02:44 -0800, Kee Nethery wrote:
> I string together a bunch of elif statements to simulate a switch
>
> if foo == True:
> blah
> elif bar == True:
> blah blah
> elif bar == False:
> blarg
> elif
This isn't what would normally be considered a switch (
hong zhang yahoo.com> writes:
>
> List,
>
> Python does not have switch statement. Any other option does similar work?
> Thanks for help.
>
> --henry
>
>
I see a couple of people have mentioned using a dictionary. If the value that
you are switching on is a string, or could be made i
Steven D'Aprano a écrit :
On Tue, 08 Dec 2009 21:36:23 -0800, Asun Friere wrote:
(snip)
It is a principle of OO design that "an object should know what to do
itself." Rather running an object though a series of tests, it is
better to send the object a message, relying on polymorphism or duck-
On Dec 8, 9:36 pm, Asun Friere wrote:
> This code is probably symptomatic of poor design. (Not to mention that
> your condition tests). For which reason python has no 'case'
> statement and why no decent OO language should.
>
> It is a principle of OO design that "an object should know what to do
On Dec 9, 5:12 pm, Steven D'Aprano
wrote:
> On Tue, 08 Dec 2009 21:02:44 -0800, Kee Nethery wrote:
> > I string together a bunch of elif statements to simulate a switch
>
> > if foo == True:
> > blah
> > elif bar == True:
> > blah blah
> > elif bar == False:
> > blarg
> > elif
>
> Ar
On Tue, 08 Dec 2009 21:36:23 -0800, Asun Friere wrote:
> On Dec 9, 4:02 pm, Kee Nethery wrote:
>> I string together a bunch of elif statements to simulate a switch
>>
>> if foo == True:
>> blah
>> elif bar == True:
>> blah blah
>> elif bar == False:
>> blarg
>> elif
>
On Tue, 08 Dec 2009 21:02:44 -0800, Kee Nethery wrote:
> I string together a bunch of elif statements to simulate a switch
>
> if foo == True:
> blah
> elif bar == True:
> blah blah
> elif bar == False:
> blarg
> elif
Are you sure you want to test for equality with True a
On Dec 9, 4:02 pm, Kee Nethery wrote:
> I string together a bunch of elif statements to simulate a switch
>
> if foo == True:
> blah
> elif bar == True:
> blah blah
> elif bar == False:
> blarg
> elif
This code is probably symptomatic of poor design. (Not to mention
I string together a bunch of elif statements to simulate a switch
if foo == True:
blah
elif bar == True:
blah blah
elif bar == False:
blarg
elif
--
http://mail.python.org/mailman/listinfo/python-list
>
> Even better (well, shorter!):
> options = {"a" : do_a, "b",do_b, "c", do_c}
> options.get(option, do_default)()
>
You can also make it something callable like so, which is a little
more compact if you need to reuse it a lot:
>>> def do_a(x): print "a:", x
...
>>> def do_b(x): print "b:", x
..
On Dec 9, 1:00 pm, Benjamin Kaplan wrote:
> On Tue, Dec 8, 2009 at 8:53 PM, hong zhang wrote:
> > List,
>
> > Python does not have switch statement. Any other option does similar work?
> > Thanks for help.
>
> Use a dict instead, where the keys are the different cases and the
> values are usually
On Tue, Dec 8, 2009 at 8:53 PM, hong zhang wrote:
> List,
>
> Python does not have switch statement. Any other option does similar work?
> Thanks for help.
>
Use a dict instead, where the keys are the different cases and the
values are usually callable objects (such as functions)
options = {"a"
On Tue, Dec 8, 2009 at 5:53 PM, hong zhang wrote:
> Python does not have switch statement. Any other option does similar work?
Yes, a dictionary with functions as values:
http://simonwillison.net/2004/May/7/switch/
Cheers,
Chris
--
http://blog.rebertia.com
--
http://mail.python.org/mailman/list
Hi JBW.
code.interact() does what I wanted. Great !!!
Thanks
N
JBW wrote:
On Mon, 16 Mar 2009 23:49:34 +0100, nntpman68 wrote:
I'd like, that a python script can be started by just calling it
(clicking on it),
but that the script can decide to enter interactive mode if certain
conditio
On Mar 16, 5:49 pm, nntpman68 wrote:
> Hi
>
> I know about two ways to enter python interactive mode
>
> 1.) just start python
>
> 2.) run python -i pythonscript.py
>
> What I am looking for is slightly different:
>
> I'd like, that a python script can be started by just calling it
> (clicking on
On Mon, 16 Mar 2009 23:49:34 +0100, nntpman68 wrote:
> I'd like, that a python script can be started by just calling it
> (clicking on it),
>
> but that the script can decide to enter interactive mode if certain
> conditions occur.
>
> Is this possible?
Don't know about the clicky-clicky part,
Check out the os module, especially the
os.getgid(),
os.getuid(),
os.setgid(),
os.getuid(),
methods. There are more , take at a look at Python documentation.
You can start a script as root then change your priveleges with
os.setgid() os.setuid() methods. Note: those methods operate with
integer us
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
Skip Montanaro <[EMAIL PROTECTED]> wrote in
news:[EMAIL PROTECTED]:
>
> 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
>
48 matches
Mail list logo