book example confusion

  • Thread starter Bruno Desthuilliers
  • Start date
B

Bruno Desthuilliers

byron a écrit :
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..

It (well... they) is (are) actually called. The parens are the call
operator.
 
B

byron

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?
 
C

Chris Rebert

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
 
J

John Machin

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?

It's not a "name assignment".
In effect it's doing this:
tmp1 = f1() # get the RESULT of calling f1()
tmp2 = f2() # likewise f2
if tmp1 or tmp2: # if result1 or result2
A (pointless) "name assignment") with the nil effect that you fear
would look like this:
tmp1, tmp2 = f1, f2 # Look, no parentheses after function names
if tmp1() or tmp2():

HTH,
John
 
B

byron

It's not a "name assignment".
In effect it's doing this:
   tmp1 = f1() # get the RESULT of calling f1()
   tmp2 = f2() # likewise f2
   if tmp1 or tmp2: # if result1 or result2
A (pointless) "name assignment") with the nil effect that you fear
would look like this:
    tmp1, tmp2 = f1, f2 # Look, no parentheses after function names
    if tmp1() or tmp2():

HTH,
John

That makes sense. Thank you for the clarification.
 
F

Fredrik Lundh

byron said:
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..

the above would be true if the code had been

tmp1, tmp2 = f1, f2

but it isn't. look again.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top