Two question from a newbie

Jason Stokes jstok at bluedog.apana.org.au
Sat Feb 12 08:45:59 EST 2000


Moshe Zadka wrote in message ...

>On Fri, 11 Feb 2000, Fred L. Drake, Jr. wrote:
>
>>  >  1.) How do I implement ORs and ANDs in an if-statment?
>>  >
>>  >    - e.g. I'd like to have this C-code in Python:
>>  >
>>  >    if (x.mode == "view" || x.mode == "modify")
>>  >       myMode = x.mode;
>>
>>     if (x.mode == "view" or x.mode == "modify"):
>>        myMode = x.mode
>
>Apparently Fred is hacking too much C these days, otherwise he'd
>encourage you to use Python (rather then C-in-Python):
>
>if x.mode in ("view", "modify"):
> myMode = x.mode


I don't see that this idiom has that much to recommend it.  Mr Drake's
version is clear in that we're comparing x.mode with two modes -- "view" and
"modify".  Your version attempts to *find* x.mode in the tuple ("view",
"modify") and executes the sub-bock if successful.  The intention of the
first example is immediately apparent.  The intention of the second is not
so immediate, so it must lose out if you want to write maximally
comprehendable code.





More information about the Python-list mailing list