python weakref模块

python weakref模块

作用

  • 对一个对象弱引用,相对于通常的引用来说,如果一个对象有一个常规的引用,它是不会被垃圾收集器销毁的,但是如果一个对象只剩下一个弱引用,那么它可能被垃圾收集器收回。
    并非所有对象都支持weakref, 例如list和dict就不支持,但是文档有说可以通过继承dict和list来支持weakref

weakref模块方法

  • class weakref.ref(object[, callback]) ,创建一个弱引用。object是被引用的对象,callback是回调函数,当引用对象被删除时,会调用回调函数

  • weakref.proxy(object[, callback]), 创建一个用弱引用实现的代理对象,参数同上;使用代理和使用普通weakref的区别就是不需要(),可以像原对象一样地使用proxy访问原对象的属性

  • weakref.getweakrefcount(object), 获取对象object关联的弱引用对象数
  • weakref.getweakrefs(object), 获取object关联的弱引用对象列表
  • class weakref.WeakKeyDictionary([dict]), 创建key为弱引用对象的字典
  • class weakref.WeakValueDictionary([dict]), 创建value为弱引用对象的字典
  • class weakref.WeakSet([elements]), 创建成员为弱引用对象的集合对象

weakref模块具有的属性

  • weakref.ReferenceType, 被引用对象的类型
  • weakref.ProxyType, 被代理对象(不能被调用)的类型
  • weakref.CallableProxyType, 被代理对象(能被调用)的类型
  • weakref.ProxyTypes, 所有被代理对象的类型序列
  • exception weakref.ReferenceError

例子:
- ref方法简介

# -*- coding=utf-8 -*-

import weakref


class Student(object):
    def __init__(self):
        self.name = 'Ricky'

    def study(self):
        print '{} study hard...'.format(self.name)


def weakref_callback(reference):
    print 'hello from callback function!'
    print reference, 'this weak reference is no longer valid'


if __name__ == '__main__':
    stu = Student()

    # ref方法,创建一个stu的弱引用
    x = weakref.ref(stu, weakref_callback)

    print x
    print x()
    print 'stu.name', x().name
    del stu


 #output:
0x7f7320e94310; to 'Student' at 0x7f7320e90a10>
<__main__.Student object at 0x7f7320e90a10>
stu.name Ricky
hello from callback function!
0x7f7320e94310; dead> this weak reference is no longer valid
  • proxy方法
# -*- coding=utf-8 -*-

import weakref


class Student(object):
    def __init__(self):
        self.name = 'Ricky'

    def study(self):
        print '{} study hard...'.format(self.name)


def weakref_callback(reference):
    print 'hello from callback function!'
    # print reference, 'this weak reference is no longer valid'
    print 'reference', reference # 使用proxy,reference打印出的是一句exception信息

if __name__ == '__main__':
    stu = Student()

    # ref方法,创建一个stu的弱引用
    x = weakref.proxy(stu, weakref_callback)

    print x
    print 'stu.name', x.name

# output:
<__main__.Student object at 0x7f4dcf480550>
stu.name Ricky
hello from callback function!
Exception ReferenceError: 'weakly-referenced object no longer exists' in 0x7f4dcf481140> ignored
reference
  • 其他方法属性简要说明
# -*- coding=utf-8 -*-

import weakref


class Student(object):
    def __init__(self):
        self.name = 'Ricky'

    def study(self):
        print '{} study hard...'.format(self.name)


if __name__ == '__main__':
    stu = Student()
    print 'getweakrefs', weakref.getweakrefs(stu)
    print 'WeakKeyDictionary', weakref.WeakKeyDictionary({stu: 'aa'})
    print 'WeakKeyDictionary', weakref.WeakValueDictionary({'aa': stu})
    print 'WeakSet', weakref.WeakSet([stu])
    print 'ReferenceType', weakref.ReferenceType
    print 'ProxyType', weakref.ProxyType

#输出
getweakrefs []
WeakKeyDictionary 140098833786280>
WeakKeyDictionary 140098833786280>
WeakSet <_weakrefset.WeakSet object at 0x7f6b4d38ddd0>
ReferenceType 'weakref'>
ProxyType 'weakproxy'>

你可能感兴趣的:(python)