Two question from a newbie

Fredrik Lundh effbot at telia.com
Sat Feb 12 10:53:29 EST 2000


Jason Stokes <jstok at bluedog.apana.org.au> wrote:
> 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.

less source code, easier to read, less error prone,
and scales much better.

in other words, a perfect (and very common) pydiom.

> The intention of the second is not so immediate, so it
> must lose out if you want to write maximally comprehendable
> code.

not if you're wearing Python goggles ;-)

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list