The following module can probably change whatever you need, not that it's a good idea to use, other than to muck around : )
Note that it will not change your source, which would probably be a disaster, especially if you make a coding error. The 'safer' option would be to play with byteplay
http://wiki.python.org/moin/ByteplayDoc
Ok, now let's play! Say we want to change the function, to print its arguments in reverse order. To do this, we will add a ROT_TWO opcode after the two arguments were loaded to the stack. See how simple it is:
>>> from byteplay import *
>>> from pprint import pprint
>>> def f(a, b):
... print (a, b)
...
>>> f(3, 5)
(3, 5)
>>> c = Code.from_code(f.func_code)
>>> c.code[3:3] = [(ROT_TWO, None)]
>>> f.func_code = c.to_code()
>>> f(3, 5)
(5, 3)
>>> f(3, 5)
(5, 3)
If you are using a defined set of options but want to preserve the same function call, you can also do something like the following
class Foo(object):
def fn(self):
pass
def op1(self):
print "HELLO"
#etc
>>> a = Foo()
>>> a.fn()
>>> a.fn = a.op1
>>> a.fn()
HELLO