zoukankan      html  css  js  c++  java
  • python weakref弱引用

    weakref号称可以解决循环引用gc和创建缓存。我困惑的是python的gc到底能不能解决循环引用的问题,有资料说python的gc是有别的辅助机制可以解决循环依赖,但是我又看见这样的代码。

    # -*- coding:utf-8 -*-
    import weakref
    import gc
    from pprint import pprint
     
     
    class Graph(object):
        def __init__(self, name):
            self.name = name
            self.other = None
     
        def set_next(self, other):
            print "%s.set_next(%r)" % (self.name, other)
            self.other = other
     
        def all_nodes(self):
            yield self
            n = self.other
            while n and n.name !=self.name:
                yield n
                n = n.other
            if n is self:
                yield n
            return
     
        def __str__(self):
            return "->".join(n.name for n in self.all_nodes())
     
        def __repr__(self):
            return "<%s at 0x%x name=%s>" % (self.__class__.__name__, id(self), self.name)
     
        def __del__(self):
            print "(Deleting %s)" % self.name
     
    def collect_and_show_garbage():
        print "Collecting..."
        n = gc.collect()
        print "unreachable objects:", n
        print "garbage:",
        pprint(gc.garbage)
     
     
    def demo(graph_factory):
        print "Set up graph:"
        one = graph_factory("one")
        two = graph_factory("two")
        three = graph_factory("three")
        one.set_next(two)
        two.set_next(three)
        three.set_next(one)
     
        print
        print "Graph:"
        print str(one)
        collect_and_show_garbage()
     
        print
        three = None
        two = None
        print "After 2 references removed"
        print str(one)
        collect_and_show_garbage()
     
        print
        print "removeing last reference"
        one = None
        collect_and_show_garbage()
     
     
    gc.set_debug(gc.DEBUG_LEAK)
    print "Setting up the cycle"
    print 
    demo(Graph)
    print
    print "breaking the cycle and cleaning up garbage"
    print
    gc.garbage[0].set_next(None)
    while gc.garbage:
        del gc.garbage[0]
    print collect_and_show_garbage()
    

    这段代码试图说明python的gc并不那么智能

  • 相关阅读:
    Python基础(一)
    计算机编程和编程语言
    初始Markdown
    Python模块
    Python递归以及面向过程编程
    Python推导式和匿名函数
    Python学闭包函数和装饰器
    Python函数的特点
    Python文件高级应用和如何使用函数
    Python字符编码和文件处理
  • 原文地址:https://www.cnblogs.com/encode/p/6378412.html
Copyright © 2011-2022 走看看