Python paradigms

Fredrik Lundh effbot at telia.com
Sun Apr 9 12:35:09 EDT 2000


Oleg Broytmann wrote:
> On Sun, 9 Apr 2000, Robert W. Cunningham wrote:
> >  blah = (((test and [true]) or [false])[0])             # blah = (test ?
> > true : false)
> >
> > Works for me!  Only if I have a good reason to suspect a problem in this
> > particular line of code will I have to look past the comment.
>
>    There is no problem in this particular line, but you'll have plenty of
> them if you'd try to use this paradigm:
>
>    blah = ((foo() and [poo()]) or [boo()])[0]
>
>    In C, "foo() ? poo() : boo()" will not call boo() if foo() returns
true;
> but in Python, you'll have a side-effect of calling boo() even if foo()
> returns true - what is wrong, usually.

did you try running the code?

python's "and" and "or" operators doesn't evaluate the
second argument unless they really have to...

</F>





More information about the Python-list mailing list