[Python-ideas] if expensive_computation() as x:
David Mertz
mertz at gnosis.cx
Sat Feb 15 01:11:31 CET 2014
A problem with the "list of expensive functions" style is that not
everything we want to do will necessarily be a pure function. I.e. if it
is, this is great:
fs = [expensive_1, expensive_2, expensive_3]
for f in fs:
x = f(known, args, here)
if x:
break
But sometimes you want more general expressions that might be expensive.
Nonetheless, using while/break gets us the early exit just as well:
while True:
x = expensive_1() + 1
if x: break
x = expensive_2() // 2
if x: break
x = expensive_3() % 3
if x: break
x = default_value
break
Honestly that isn't very verbose. And also, while the def/return approach
is similar, it *does* require passing in all the relevant lexical elements
needed into the function (well, or using a closure), and that's a bit more
bookkeeping possibly.
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140214/b3439606/attachment.html>
More information about the Python-ideas
mailing list