switch

Asun Friere afriere at yahoo.co.uk
Wed Dec 9 01:43:54 EST 2009


On Dec 9, 5:12 pm, Steven D'Aprano
<ste... at REMOVE.THIS.cybersource.com.au> 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 ....
>
> Are you sure you want to test for equality with True and False? Generally
> one should write that as:
>
> if foo:
>     blah
> elif bar:
>     blah blah
> elif not bar:
>     blarg
> ...
>
> --
> Steven

I was going to point that out, but thought it a little OT.  One might
also mention that testing for "if foo is None :" is a special case.
I'm also having a bit of a problem imagining what the subsequent
conditions must be which make testing "elif not bar" subsequent to
testing "elif bar" necessary, but that's just me.

Back OT, one would hope not to encounter python code with a long chain
of elifs like that.  Probably the design should be improved, or where
this would be overkill, use the dictionary trick.




More information about the Python-list mailing list