This post originated from an RSS feed registered with Python Buzz
by maxim khesin.
Original Post: to throw, definitely
Feed Title: python and the web
Feed URL: http://feeds.feedburner.com/PythonAndTheWeb
Feed Description: blog dedicated to python and the networks we live in
So Jeremy (my intro here) replied to my RFC. It appears putting a try-catch with an agreed-upon exception right into the bfs is a cool idea. So basically we would have something like
class traversal_termination_exception:pass
def bfs(graph, start, visitor): try: # actual bfs here except traversal_termination_exception: pass # this is a normal termination, nothin' to do
I was thinking that once we got to this point, we could take the next step: return a value from bfs. Why would we need this? Say our we want to search for the Bacon Number between Charlie Chaplin and Britney Spears. The result of this operation is a distance number, stored away in the visitor class. Since we decided to terminate the traversal with an exception when we reach Britney, why not stash the return value in the exception? With a little change, we have this:
class traversal_termination_exception: def __init__(self, val = None): self.val = val def get_val(self): return self.val
def bfs(graph, start, visitor): try: # actual bfs here except traversal_termination_exception, e: return e.get_val() return None # for consistency?