On Thu, 03 Jan 2008 16:56:00 -0600, Reedick, Andrew wrote:

> The problem occurred because a method used to generate keys was
> returning a string instead of a number without an explicit conversion
> taking place.  And since I was using hash.get(i, default_value) to avoid
> having to pair every key lookup with a hash.has_key(), no exception was
> thrown when the key wasn't found.


# How to fix this broken function without modifying the source?
def foo(arg):
    """Returns a string instead of a number."""
    return "1"  # oops I meant 1



_foo = foo  # save a reference to original broken function
foo = lambda *args, **kwargs: int(_foo(*args, **kwargs))  # and patch it


And now you can use foo(arg) confident that it will always return an int 
like you expect.

Modifications of this technique should be obvious.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to