< October 2006 >
1 2 3 4 5 6 7
8 91011121314
Fri, 13 Oct 2006:

I was battling with some code that used instanceof() in python. I don't where that's a built-in (I suspect jython), but it doesn't work on my python 2.4.3. While I was wondering about this problem, I tried something outlandish and it worked. But before I actually explain what I did, take a closer look at this module (x.py).

def identity(h):
    return h

def coolfunc(p):
    return identity(p)

That was pretty standard code, right ? Now, to use/abuse this module in y.py.

import x

print x.coolfunc("Hello World !")

x.identity = lambda x : 42;

print x.coolfunc("What is 21 + 21 ?")

As you can probably guess, I can override (uhh.., pollute) x.identity with my own functions, which opens up a solution to my original problem. Pretty simple, now that I think of it.

othermodule.instanceof = lambda x,y : type(x) == y

I hope this has made your friday a little bit more surreal :)

Talent to endure stems from the ignorance of alternatives.

posted at: 10:42 | path: /hacks | permalink | Tags: ,