#! python def gen(n): for i in range(n): yield i print 'CLEANUP' print "The user would expect 'CLEANUP' to be printed twice:" print '4, 4' zip(gen(4), gen(4)) print print "The user might expect 'CLEANUP' to be printed twice:" print '3, 4' zip(gen(3), gen(4)) print print "The user might expect 'CLEANUP' to be printed twice:" print '4, 3' zip(gen(4), gen(3)) print 'Overall, only half of the "CLEANUP" calls get executed.' class withdel(object): def __init__(self): self.x = "huge data" def __del__(self): print "Deallocate huge data"