book example confusion

Chris Rebert cvrebert at gmail.com
Fri Sep 12 15:45:20 EDT 2008


Note the parentheses after f1 and f2 in the second example. That's  
what calls the functions and causes them to be evaluated and run.

- Chris

Sent from my iPod

On Sep 12, 2008, at 12:36 PM, byron <bjruth at gmail.com> wrote:

> I am reading o'reilly's learning python (great book), but i came
> across an example (pg 291, pdf) that I am not quite understanding the
> reasoning for the author's explanation:
>
> if f1() or f2():
>
> The author states that do to the nature of that expression, if f1()
> returns True, f2() will not be evaluated.. which makes sense. His
> quote:
>
>        "Here, if f1 returns a true (or nonempty) value, Python will
> never run f2."
>
> He then states:
>
>        "To guarantee that both functions will be run, call them
> before the 'or':"
>
> tmp1, tmp2 = f1(), f2()
> if tmp1 or tmp2:
>
> Being that each function is an object, a name assignment to
> (tmp1,tmp2) doesn't actually evaluate or run the function itself until
> the name is called.. so why would the latter example "run" both
> functions as the author suggests?
> --
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list