secret
Created

  • Download Gist
wtf.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
import gc
import pprint
import weakref
 
REFS = weakref.WeakKeyDictionary()
 
 
class SimpleAdder(object):
def __init__(self, name):
self.name = name
REFS[self] = self.name
 
def __repr__(self):
return 'Adder %s' % self.name
 
def add(self, n):
return n + 1
 
 
class AdvancedAdder(SimpleAdder):
def __init__(self, name, add_one=True):
if add_one:
self.add_fn = self.add_one
else:
self.add_fn = self.add_two
super(AdvancedAdder, self).__init__(name)
 
def add_one(self, n):
return n + 1
 
def add_two(self, n):
return n + 2
 
def add(self, n):
return self.add_fn(n)
 
 
def add_some_things():
s1 = SimpleAdder('s1')
print s1.add(0)
 
s2 = SimpleAdder('s2')
print s2.add(1)
 
a1 = AdvancedAdder('a1')
print a1.add(2)
 
a2 = AdvancedAdder('a2', False)
print a2.add(2)
 
a3 = AdvancedAdder('a3')
print a3.add(3)
 
a4 = AdvancedAdder('a4')
 
 
add_some_things()
 
print 'What has not been gc-ed?'
print REFS.keys()
 
print 'Well why the hell not?'
for ref in REFS:
print ref
pprint.pprint(gc.get_referrers(ref))
print
 
print 'Lets take out the trash'
gc.collect()
 
print 'Anything there?'
print REFS.keys()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.