Luke Lee

Software Engineer

Web + Desktop + Science

Fork me on Github

Monkey-patching for good

Monkey-patching generally has a negative connotation in the Python community. Luckily, the community values code that is straight-forward and explicit, which is typically the opposite of monkey-patching.

However, there is a time and a place for everything. In fact, Ned Batchelder shows off a very clever use in his excellent post about a recent bug chase. He replaces the function random.random with lambda: 1/0 which cleverly triggers an exception when someone later in the code tries to use random.random. The real beauty of this 'solution' is the exception and automatic stack trace. This can be very valuable when debugging code that you're very familiar with.

The article is a great read so be sure to check it out.

Published: 02-18-2013 14:34:19

lukelee.net