Sometimes I find myself writing code like this:
def analyse(somedata):
result = bestapproach(somedata)
if result:
return result
else:
result = notasgood(somedata)
if result:
return result
else:
result = betterthannothing(somedata)
if result:
return result
else:
return None
That's pretty ugly. Of course, some people like to leave off some of that syntax:
def analyse(somedata):
result = bestapproach(somedata)
if result:
return result
result = notasgood(somedata)
if result:
return result
result = betterthannothing(somedata)
if result:
return result
But that's not much of an improvement; there's still a ton of duplicated code here, and it's still ugly.
I looked into using the built-in iter()
with a sentinel value, but in this case the None
value is being used to signal that the loop should keep going, as opposed to a sentinel which is used to signal that the loop should terminate.
Are there any other (sane) techniques in Python for implementing this sort of "keep trying until you find something that works" pattern?
I should clarify that "return value meets some condition" is not limited to cases where the condition is if bool(result) is True
as in the example. It could be that the list of possible analysis functions each produce some coefficient measuring the degree of success (e.g. an R-squared value), and you want to set a minimum threshold for acceptance. Therefore, a general solution should not inherently rely on the truth value of the result.
Option #1: Using
or
When the number of total functions is a) known, and b) small, and the test condition is based entirely on the truth value of the return, it's possible to simply use
or
as Grapsus suggested:Because Python's boolean operators short-circuit, the functions are executed from right to left until one of them produces a return value evaluated as
True
, at which point the assignment is made toresult
and the remaining functions are not evaluated; or until you run out of functions, andresult
is assignedFalse
.Option #2: Using generators
When the number of total functions is a) unknown, or b) very large, a one-liner generator comprehension method works, as Bitwise suggested:
This has the additional advantage over option #1 that you can use any
<test-condition>
you wish, instead of relying only on truth value. Each time.next()
is called:f
fromfunctions
and tries to evaluatef(somedata)
f
is a function andsomedata
is a valid arugment), the inner generator yields the return value off(somedata)
to the outer generator<test-condition>
is satisfied, the outer generator yields the return value off(somedata)
and we assign it asresult
<test-condition>
was not satisfied in step 5, repeat steps 2-4A weakness of this method is that nested comprehensions can be less intuitive than their multi-line equivalents. Also, if the inner generator is exhausted without ever satisfying the test condition,
.next()
raises aStopIteration
which must be handled (in a try-except block) or prevented (by ensuring the last function will always "succeed").Option #3: Using a custom function
Since we can place callable functions in a list, one option is to explicitly list the functions you want to "try" in the order they should be used, and then iterate through that list:
Advantages: Fixes the problem of repeated code, it's more clear that you're engaged in an iterative process, and it short-circuits (doesn't continue executing functions after it finds a "good" result).
This could also be written with Python's
for ... else
syntax:*The advantage here is that the different ways to exit the function are identified, which could be useful if you want complete failure of the
analyse()
function to return something other thanNone
, or to raise an exception. Otherwise, it's just longer and more esoteric.*As described in "Transforming Code into Beautiful, Idiomatic Python", starting @15:50.
This is pretty pythonic:
The idea is to use generators so you do lazy evaluation instead of evaluating all functions. Note that you can change the
condition
/funcs
to be whatever you like, so this is more robust than theor
solution proposed by Grapsus.This is a good example why generators are powerful in Python.
A more detailed description of how this works:
We ask this generator for a single element. The outer generator then asks the inner generator
(f(d) for f in funcs)
for a single element, and evaluates it. If it passes the condition then we are done and it exits, otherwise it continues asking the inner generator for elements.If the number of functions is not too high, why not use the
or
operator ?It will only apply the functions until one of them returns something not
False
.