Created
August 6, 2013 17:38
-
-
Save telepenin/6166692 to your computer and use it in GitHub Desktop.
gc and weakref
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "metadata": { | |
| "name": "gc_weakref" | |
| }, | |
| "nbformat": 3, | |
| "nbformat_minor": 0, | |
| "worksheets": [ | |
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \\__new__ \u0438 \\__init__" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "class Circle(object):\n def __init__(self, radius):\n self.radius = radius", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 58 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "c = Circle(3.0)", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 59 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "d = Circle(4.0)", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 60 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "e = Circle.__new__(Circle, 5.0)\nif isinstance(e, Circle):\n Circle.__init__(e, 5.0)", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 61 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "e.radius", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 62, | |
| "text": "5.0" | |
| } | |
| ], | |
| "prompt_number": 62 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "\u041d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0442 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0445 \u0442\u0438\u043f\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u0443\u0441\u043f\u0435\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0412 \\__init__ \u0431\u044b\u043b\u043e \u0431\u044b \u043f\u043e\u0437\u0434\u043d\u043e" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "class Upperstr(str):\n def __new__(cls, value=\"\"):\n return str.__new__(cls, value.upper())", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 63 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "Upperstr(\"hello\")", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 64, | |
| "text": "'HELLO'" | |
| } | |
| ], | |
| "prompt_number": 64 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "\\__del__ \u0437\u0430\u0447\u0435\u043c \u043d\u0430\u0434\u043e \u0438 \u043a\u043e\u0433\u0434\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f\n\u0421\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432: \u043f\u043e\u0434\u0441\u0447\u0435\u0442 \u0441\u0441\u044b\u043b\u043e\u043a \u0438 \u0441\u0431\u043e\u0440\u043a\u0430 \u043c\u0443\u0441\u043e\u0440\u0430.\n\u0418\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f del" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "class Account(object):\n def __init__(self, name, balance):\n self.name = name\n self.balance = balance\n self.observers = set()\n \n def __del__(self):\n for ob in self.observers:\n ob.close()\n del self.observers\n \n def register(self, observer):\n self.observers.add(observer)\n \n def unregister(self, observer):\n self.observers.remove(observer)\n \n def notify(self):\n for ob in self.observers:\n ob.notify()\n \n \nclass AccountObserver(object):\n def __init__(self, account):\n self.account = account\n account.register(self)\n \n def __del__(self):\n self.account.unregister(self)\n del self.account\n \n def update(self):\n print(\"Balance: %0.2f\" % self.account.balance)\n \n def close(self):\n print(\"Observer disconnected\")", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 65 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "a = Account(\"one\", 1000.00)\na_ob = AccountObserver(a)", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 66 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "\u041c\u043e\u0434\u0443\u043b\u044c gc \u0438 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u043e\u0442\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "import gc", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 67 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "gc.collect()", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 68, | |
| "text": "411" | |
| } | |
| ], | |
| "prompt_number": 68 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "gc.garbage", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 69, | |
| "text": "[<__main__.Account at 0x10269c350>,\n <__main__.AccountObserver at 0x10269c7d0>,\n <__main__.Account at 0x10269cdd0>,\n <__main__.AccountObserver at 0x10269cd90>,\n <__main__.Account at 0x1026a61d0>,\n <__main__.AccountObserver at 0x1026a6190>,\n <__main__.AccountObserver at 0x1026988d0>,\n <__main__.Account at 0x102698910>]" | |
| } | |
| ], | |
| "prompt_number": 69 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "class Node(object):\n parent = None\n\n def __init__(self, *children):\n self.children = list(children)\n for node in self.children:\n node.parent = self\n\n @classmethod\n def tree(cls, depth=1, numchildren=1):\n if depth == 0:\n return []\n return [cls(*cls.tree(depth-1, numchildren)) for _ in range(numchildren)]", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 70 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "import gc\n\ndef gc_cb(phase, info):\n if not info['collected'] and not info['uncollectable']:\n return\n print(\"{0}:\\t{1[generation]}\\t{1[collected]}\\t{1[uncollectable]}\".format(\n phase, info))\n\ngc.callbacks.append(gc_cb)", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 71 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "# \u0411\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u043e 9330 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0430 1 \u043f\u0440\u043e\u0445\u043e\u0434\nfor n in range(20):\n for _ in range(n):\n Node.tree(depth=5, numchildren=6)", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "stop:\t0\t464\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t107295\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t107295\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t102630\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t102630\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t107295\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t107295\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t107295\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t107295\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t97965\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t97965\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t102630\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t102630\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t102630\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t102630\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t97965\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t2\t97965\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t102630\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t107295\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t74640\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t2\t79305\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\n" | |
| } | |
| ], | |
| "prompt_number": 72 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "weakref" | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "import weakref\n\nclass WeakNode(Node):\n parent = None\n\n def __init__(self, *children):\n self.children = list(children)\n for node in self.children:\n node.parent = weakref.proxy(self)", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 73 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "class WeakNode(Node):\n _parent = None\n\n def __init__(self, *children):\n self.children = list(children)\n for node in self.children:\n node._parent = weakref.ref(self)\n\n @property\n def parent(self):\n if self._parent is None:\n return None\n else:\n return self._parent()", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 74 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "# \u0411\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u043e 9330 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0430 1 \u043f\u0440\u043e\u0445\u043e\u0434\nfor n in range(20):\n for _ in range(n):\n WeakNode.tree(depth=5, numchildren=6)", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "stop:\t0\t458\t0\nstop:\t2\t27990\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\n" | |
| } | |
| ], | |
| "prompt_number": 75 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "gc.set_threshold(10000, 100, 100)\n#gc.set_threshold(10000, 10, 10)", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 76 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "# \u0411\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u043e 9330 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u043d\u0430 1 \u043f\u0440\u043e\u0445\u043e\u0434\nfor n in range(20):\n for _ in range(n):\n Node.tree(depth=5, numchildren=6)", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "stop:\t0\t34\t0\nstop:\t0\t4665\t0\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t9330\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t9330\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t881685\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0\nstop:\t0\t9330\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t9330\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t9330\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t909675\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0\nstop:\t0\t9330\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t9330\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t900345\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0\nstop:\t0\t9330\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t9330\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t9330\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t909675\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t9330\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t9330\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t1\t900345\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0\nstop:\t0\t9330\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0" | |
| }, | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": "\nstop:\t0\t4665\t0\n" | |
| } | |
| ], | |
| "prompt_number": 77 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "import weakref\n\nclass AccountObserver(object):\n def __init__(self, account):\n self.account_ref = weakref.ref(account)\n account.register(self)\n \n def __del__(self):\n account = self.account_ref()\n if account:\n self.account.unregister(self)\n \n def update(self):\n print(\"Balance: %0.2f\" % self.account.balance)\n \n def close(self):\n print(\"Observer disconnected\")", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 78 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "a = Account(\"two\", 20.00)\na_ob = AccountObserver(a)", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 79 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "import gc", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 80 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": "gc.garbage", | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "pyout", | |
| "prompt_number": 81, | |
| "text": "[<__main__.Account at 0x10269c350>,\n <__main__.AccountObserver at 0x10269c7d0>,\n <__main__.Account at 0x10269cdd0>,\n <__main__.AccountObserver at 0x10269cd90>,\n <__main__.Account at 0x1026a61d0>,\n <__main__.AccountObserver at 0x1026a6190>,\n <__main__.AccountObserver at 0x1026988d0>,\n <__main__.Account at 0x102698910>]" | |
| } | |
| ], | |
| "prompt_number": 81 | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": "\u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c:\n\n* [\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0441\u0431\u043e\u0440\u0449\u0438\u043a\u043e\u043c \u043c\u0443\u0441\u043e\u0440\u0430 - \u0431\u043b\u043e\u0433 \u0410\u043d\u0434\u0440\u0435\u044f \u0421\u0432\u0435\u0442\u043b\u043e\u0432\u0430](http://asvetlov.blogspot.ru/2013/05/gc.html)\n* [\u0423\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0443\u0442\u0435\u0447\u0435\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u043d\u0430 \u041f\u0438\u0442\u043e\u043d\u0435 - \u0441\u0442\u0430\u0442\u044c\u044f \u043d\u0430 \u0445\u0430\u0431\u0440\u0435](http://habrahabr.ru/post/178637/)\n* \u0414\u044d\u0432\u0438\u0434 \u0411\u0438\u0437\u043b\u0438 - Python. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u044b\u0439 \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a\n* [weakref docs v3.4a](http://docs.python.org/3.4/library/weakref.html)\n* [gc docs v3.4a](http://docs.python.org/3.4/library/gc.html)" | |
| } | |
| ], | |
| "metadata": {} | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment