Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'else:': 0.03; 'url:pypi': 0.03; 'attribute': 0.05; 'none:': 0.05; 'that?': 0.05; 'decorator': 0.07; '@property': 0.09; 'compute': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'wrote:': 0.17; 'stefan': 0.17; 'trying': 0.21; 'cc:2**0': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'skip:@ 10': 0.27; 'received:192.168.1.3': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'url:python': 0.32; 'could': 0.32; 'problem': 0.33; 'code:': 0.33; 'subject:?': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 0.36; 'expensive': 0.36; 'useful': 0.36; 'should': 0.36; 'charset:us-ascii': 0.36; 'quite': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'header:Message-Id:1': 0.62; 'received:at': 0.71; 'andrea': 0.84 Subject: Re: lazy properties? Mime-Version: 1.0 (Apple Message framework v1085) Content-Type: text/plain; charset=us-ascii From: "Stefan H. Holek" In-Reply-To: <5092EBED.2090002@gmail.com> Date: Fri, 2 Nov 2012 09:36:11 +0100 Content-Transfer-Encoding: quoted-printable References: <5092EBED.2090002@gmail.com> To: Andrea Crotti X-Mailer: Apple Mail (2.1085) Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351845374 news.xs4all.nl 6983 [2001:888:2000:d::a6]:33825 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32601 On 01.11.2012, at 22:38, Andrea Crotti wrote: > Seeing the wonderful "lazy val" in Scala I thought that I should try = to get the following also in Python. > The problem is that I often have this pattern in my code: >=20 > class Sample: > def __init__(self): > self._var =3D None >=20 > @property > def var(self): > if self._var is None: > self._var =3D long_computation() > else: > return self._var >=20 >=20 > which is quite useful when you have some expensive attribute to = compute that is not going to change. > I was trying to generalize it in a @lazy_property but my attempts so = far failed, any help on how I could do that? There is a ready made and well tested lazy decorator at = http://pypi.python.org/pypi/lazy Stefan --=20 Stefan H. Holek stefan@epy.co.at