Skip to content

Latest commit

 

History

History
39 lines (29 loc) · 1.17 KB

day18.md

File metadata and controls

39 lines (29 loc) · 1.17 KB

第十八天

索引

python标准库 weakref

  1. 建立
import weakref


class ExpensiveObject:

    def __del__(self):
        print('(Deleting {})'.format(self))


def callback(reference):
    """Invoked when referenced object is deleted"""
    print('callback({!r})'.format(reference))


obj = ExpensiveObject()
r = weakref.ref(obj, callback)

print('obj:', obj)
print('ref:', r)
print('r():', r())

print('deleting obj')
del obj
print('r():', r())
  1. 好的文章