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? 4 760
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 <bj****@gmail.comwrote:
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
On Sep 13, 5:36*am, byron <bjr...@gmail.comwrote:
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
On Sep 12, 3:51*pm, John Machin <sjmac...@lexicon.netwrote:
On Sep 13, 5:36*am, byron <bjr...@gmail.comwrote:
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, Pythonwill
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
That makes sense. Thank you for the clarification.
byron wrote:
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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Grant Edwards |
last post by:
Can anybody recommend a good book on wxPython? Are there any
books on wxPython?
I've been trying to learn wxPython and/or wax for a few weeks,
and I'm just not getting it.
wxWindows seems...
|
by: Scott Metzger |
last post by:
Hi,
I am looking for a good book on C# and dot net.
I have 10 yrs programming experience. Mostly with C++, databases, and
GUI front ends in windows.
I don't have alot of money to buy a whole...
|
by: Mike |
last post by:
Hi,
I am planning on purchasing VS2005 to learn C# very soon, and I need good
book recommendations. I realize this is a question that may be asked a lot,
but please consider my background:
I...
|
by: Malcolm McLean |
last post by:
The webpages for my new book are now up and running.
The book, Basic Algorithms, describes many of the fundamental algorithms
used in practical programming, with a bias towards graphics. It...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |