Skip to content

Instantly share code, notes, and snippets.

@wraithan
Created October 12, 2011 01:03
Show Gist options
  • Select an option

  • Save wraithan/1279926 to your computer and use it in GitHub Desktop.

Select an option

Save wraithan/1279926 to your computer and use it in GitHub Desktop.
Evil Pure Python Evil
def speak():
print('woof')
def speak():
print('meow')
import inspect
import example1 as example
def replace():
# doesn't work because of scoping
import example2 as example
def inspect_replace():
current_frame = inspect.currentframe()
# Essentially the same as 'u' in pdb
outer_frame = current_frame.f_back
import example2
outer_frame.f_globals['example'] = example2
# Have to delete any references to the frames or else they wont be
# properly garbage collected
del current_frame
del outer_frame
example.speak()
replace()
example.speak()
inspect_replace()
example.speak()
$ python2 replace.py
woof
woof
meow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment