Ubuntu Pastebin

Paste from Robert Bruce Park at Wed, 28 Oct 2015 20:09:44 +0000

Download as text
 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
$ cat foo.py 
class Foo:
    def __del__(self):
        print('freed', self)

def make():
    for i in range(5):
        yield Foo()

from sys import getrefcount as rc

for obj in make():
    # refcount should == 2, one for `obj` binding and other for the argument
    # binding in rc().
    print(rc(obj))

$ python3 foo.py 
2
freed <__main__.Foo object at 0x7f38b17b6a90>
2
freed <__main__.Foo object at 0x7f38b17b6b38>
2
freed <__main__.Foo object at 0x7f38b17b6a90>
2
freed <__main__.Foo object at 0x7f38b17b6b38>
2
freed <__main__.Foo object at 0x7f38b17b6a90>
Download as text