Per Erik Strandberg /cv /kurser /blog

For more on monkey patches see [1].

If you end up not happy with the way someone has implemented something you can just overwrite it with whatever you want. This is a monkey patch, also known as gorilla patch, also known as guerilla patch (because they fight a war on what code to be used).

In the wikipedia article on monkey patches they mention the synonym "duck punching". This is incredibly funny. To understand the joke you must understand the concept of duck typing (see [2]): "If it walks like a duck and quacks like a duck, I would call it a duck". With a monkey patch you can expand the saying into: If it does not walks and talk like a duck then punch it until it does.

Silly example

Suppose I have a class foobie with a method bletch

class foobie:
    def bletch(self):
        print "foobie bletch"

But I want to replace it with another function called blatch. So I just do it:

def blatch(self):
    print "I am the monkey that will rule the world"

foobie.bletch = blatch

My little test program looks like this:

maud = foobie()
maud.bletch()

And the output is:

I am the monkey that will rule the world

This is horrible, or wonderful, or both at the same time. I really do not know...


This page belongs in Kategori Programmering