I'm designing a tool which recursively identifies upstream pathways in a vector river network. I'd like to be able to truncate the search if the cumulative path cost passes a test which is defined by the user, and I'm not sure whether this is possible. Below is an example of the sort of thing I'd like to do:
def test1(value):
if value > 1: return True
else: return False
def test2(value):
if value % 4: return True
else: return False
def main(test):
for i in range(20):
if SPECIFIEDTEST(i):
print i
break
main(test1)
I know exec() can be used for this purpose but I also understand this is frowned upon? Is there another, better method for passing a function to another function?
f = {'test1':test1, 'test2':test2}[key]? (after whichf(i)calls the desired function)def test1(value): return value > 1, etc. You rarely need to use the actual literalsTrueandFalse.