Skip to content

Instantly share code, notes, and snippets.

@graingert
Last active January 4, 2016 01:49
Show Gist options
  • Save graingert/8550600 to your computer and use it in GitHub Desktop.
Save graingert/8550600 to your computer and use it in GitHub Desktop.
import gc
def super_patch(old_obj, new_obj):
if old_obj is new_obj:
return
gc.collect()
for referrer_obj in gc.get_referrers(old_obj):
if hasattr(referrer_obj, "iteritems"):
for key, value in referrer_obj.iteritems():
if value is old_obj:
referrer_obj[key] = new_obj
@graingert
Copy link
Author

Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from super_patch import super_patch
>>> from foouser import bar
>>> from foo import foo
>>> bar()
4
>>> super_patch(foo, lambda: 3)
>>> bar()
3
>>> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment