short-circuit behavior anomaly?
Terry Reedy
tejarex at yahoo.com
Sat Mar 9 02:24:17 EST 2002
"logosity" <wecaputo at thoughtworks.com> wrote in message
news:mailman.1015657336.24581.python-list at python.org...
> Hi,
>
> I was working through this tutorial:
> http://gnosis.cx/publish/programming/charming_python_13.txt
>
> Which shows the statements in the following functions (imp and fn)
as
> equivalent:
>
> def pr(s): print s
By default, functions return None, which is false. For this function
to work right in context below, you must return a true value, such as
1. If you have copied from tutorial accurately, it is incorrect and
you should report bug.
> def imp():
> if x == 1: pr('one')
> elif x == 2: pr('two')
> else: pr('other')
>
> def fn():
> (x == 1 and pr('one')) \
> or (x == 2 and pr('two')) \
> or (pr('other'))
When pr return None, no 'short-circuit occurs and pr('other') is
called regardless of value of x. I personally use and/or to simulate
conditional expressions, but one *must* be careful when doing so.
Terry J. Reedy
More information about the Python-list
mailing list