Skip to content

Instantly share code, notes, and snippets.

@mithrandi
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mithrandi/7ee6021ca385b15a5f4f to your computer and use it in GitHub Desktop.
Save mithrandi/7ee6021ca385b15a5f4f to your computer and use it in GitHub Desktop.
weakref example
mithrandi@lorien /tmp> pypy weakrefs.py
before disable: w() = <__main__.C object at 0x00007f5e52d41c58>
after del: w() = <__main__.C object at 0x00007f5e52d41c58>
after collect: w() = None
hi mom
after enable: w() = None
from weakref import ref
from gc import collect, enable, disable
class C(object): pass
def cb(x):
print 'hi mom'
def test():
o = C()
w = ref(o, cb)
print 'before disable: w() =', w()
disable()
del o
print 'after del: w() =', w()
collect()
print 'after collect: w() =', w()
enable()
print 'after enable: w() =', w()
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment