-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathweakref_tutorial.py
48 lines (37 loc) · 1.11 KB
/
weakref_tutorial.py
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
# ref.
# https://docs.python.org/3/library/weakref.html#finalizer-objects
# https://docs.python.org/3/library/weakref.html#module-weakref
# A primary use for weak references is to implement caches or mappings holding large objects,
# where it’s desired that a large object not be kept alive solely
# because it appears in a cache or mapping.
# for garbage collector(GC)
import weakref
class Object:
pass
def example_1():
twtrubiks = Object()
weakref.finalize(twtrubiks, print, "You killed twtrubiks!")
del twtrubiks
def callback(x, y, z):
print("hello")
return x + y + z
def example_2():
obj = Object()
f = weakref.finalize(obj, callback, 1, 2, 3)
print('f()', f())
def example_3():
# With WeakValueDictionary garbage collection can reclaim the object
# when there are no other references to it.
class Foo(object):
pass
A = Foo()
B = weakref.ref(A)
# B = A
# <weakref at 0x108b43958; to 'Foo' at 0x108b2f468>
del A
print(B)
# <weakref at 0x108b43958; dead>
if __name__ == "__main__":
example_1()
# example_2()
# example_3()