Thursday, July 2, 2009

Using an inner function for breaking out of nesting

From Fuzzyman, a blog post on different ways of handling breaking out of nesting. Here is a snippet:
def find_match():
    for x in range(max_x):
        for y in range(max_y):
            if match(x, y):
                return x, y
result = find_match()
if result is None:
    # match not found
    x, y = result
Yup, looks clean to me, and the named "result" and "inner_function" give the opportunity for self-documentation, with appropriate names instead of "result" and "inner_function".

Great Blue Heron pair preparing a nest (bird),...Image by mikebaird via Flickr

For your esthetical enrichment, a picture of a nest!
Reblog this post [with Zemanta]

No comments: