Last active
August 29, 2015 13:56
-
-
Save croepha/9278416 to your computer and use it in GitHub Desktop.
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": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"See http://stackoverflow.com/questions/3012421/python-lazy-property-decorator#comment33530512_6849299" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def lazyprop(fn):\n", | |
" attr_name = '_lazy_' + fn.__name__\n", | |
" @property\n", | |
" def _lazyprop(self):\n", | |
" if not hasattr(self, attr_name):\n", | |
" setattr(self, attr_name, fn(self))\n", | |
" return getattr(self, attr_name)\n", | |
" return _lazyprop\n", | |
"\n", | |
"\n", | |
"class Test(object):\n", | |
"\n", | |
" @lazyprop\n", | |
" def a(self):\n", | |
" print 'generating \"a\"'\n", | |
" return range(5)\n", | |
" \n", | |
"t = Test()\n", | |
"b = t.a\n", | |
"%timeit b = t.a" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"generating \"a\"\n", | |
"100000 loops, best of 3: 2.45 \u00b5s per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class lazy_property(object):\n", | |
" '''\n", | |
" meant to be used for lazy evaluation of an object attribute.\n", | |
" property should represent non-mutable data, as it replaces itself.\n", | |
" '''\n", | |
"\n", | |
" def __init__(self,fget):\n", | |
" self.fget = fget\n", | |
" self.func_name = fget.__name__\n", | |
"\n", | |
"\n", | |
" def __get__(self,obj,cls):\n", | |
" if obj is None:\n", | |
" return None\n", | |
" value = self.fget(obj)\n", | |
" setattr(obj,self.func_name,value)\n", | |
" return value\n", | |
"class Test(object):\n", | |
"\n", | |
" @lazy_property\n", | |
" def a(self):\n", | |
" print 'generating \"a\"'\n", | |
" return range(5)\n", | |
" \n", | |
"t = Test()\n", | |
"b = t.a \n", | |
"%timeit b = t.a" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"generating \"a\"\n", | |
"1000000 loops, best of 3: 322 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment