import inspect | |
def marmoset_patch(func, s, r): | |
source = inspect.getsource(func).replace(s, r) | |
exec source in func.func_globals | |
func.func_code = func.func_globals[func.__name__].func_code | |
def foo(): | |
print 1 | |
print 2 | |
print 3 | |
foo() | |
# 1 | |
# 2 | |
# 3 | |
marmoset_patch(foo, '3', "'ZOMG'") | |
foo() | |
# 1 | |
# 2 | |
# ZOMG |
This comment has been minimized.
This comment has been minimized.
gldnspud
commented
Apr 6, 2012
Too late... you let the marmoset out of the bag! |
This comment has been minimized.
This comment has been minimized.
khs
commented
Apr 7, 2012
This seems like a good idea. Thank you for helping a new programmer out with learning new techniques. :) More seriously, what is monkey patching? |
This comment has been minimized.
This comment has been minimized.
codlark
commented
Apr 7, 2012
@kellerkeller |
This comment has been minimized.
This comment has been minimized.
ctheune
commented
Mar 19, 2013
O M G |
This comment has been minimized.
This comment has been minimized.
sureshvv
commented
Jun 23, 2013
Have the python people heard of this? |
This comment has been minimized.
This comment has been minimized.
miohtama
commented
Feb 17, 2014
This technique intrigues me. I look forward to apply it in many real-life scenarios. |
This comment has been minimized.
This comment has been minimized.
ztane
commented
Feb 17, 2014
This is so wrong, the right solution is to use AST rewriting! |
This comment has been minimized.
davisagli commentedApr 6, 2012
Please, don't actually do this.