Last active
August 29, 2015 14:13
-
-
Save mithrandi/7ee6021ca385b15a5f4f to your computer and use it in GitHub Desktop.
weakref example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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