How to code a series of alternatives

Aahz aahz at pythoncraft.com
Mon Feb 23 13:41:34 EST 2004


In article <Xns94987ED355A3Asdfexpertunecom at 216.168.3.44>,
Scott F  <sdfATexpertuneDOTcom> wrote:
>testvar = bob
>
># Trial A: This does not work
>if testvar == ("fred" or "bob):
>    statements
>
># Trial B: This does work
>if textvar == "fred" or textvar == "bob":
>    statements
>
>Trial B is OK for 1 or 2 tests, but gets messy for 5 or 6.  What is the 
>variation of Trial A that will work?  

Simplest:

    if testvar in ('fred', 'bob'):

Fastest:

    options = sets.Set('fred', 'bob'):

        ...

    if testvar in options:
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Do not taunt happy fun for loops. Do not change lists you are looping over."
--Remco Gerlich, comp.lang.python



More information about the Python-list mailing list