Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'value,': 0.04; 'instance,': 0.05; 'descriptor': 0.09; '25,': 0.12; 'am,': 0.13; 'wrote:': 0.15; 'subject:memory': 0.16; 'cc:addr:python-list': 0.16; 'mon,': 0.16; 'pm,': 0.16; 'def': 0.16; 'cc:2**0': 0.21; 'cc:no real name:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'fri,': 0.28; 'message-id:@mail.gmail.com': 0.28; 'cc:addr:python.org': 0.30; 'kelly': 0.30; 'class': 0.31; 'subject:?': 0.31; 'received:209.85.161.46': 0.34; 'received:mail- fx0-f46.google.com': 0.34; 'identical': 0.35; 'try:': 0.35; 'received:google.com': 0.38; 'received:209.85.161': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'except': 0.39; 'received:209': 0.40; 'called': 0.40; '11:46': 0.84; 'subject:Identical': 0.84; 'subject:value': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=WjCESk0Rp7y0BVolaWq8fheepcpS0vbVF+dLPPnIg8U=; b=nvyHngzyThhomr82TT3TLjhZEQL2q4IthUtcbDjHq6HAGGj98hlWgoDCjmxYG65TzF 5fDuBtM4bFCJtG4fsQPNkd2n83gx2FxVUZ6cUhm8cCA403H6iuH9BQNO967dybLY8AGY 8OWHRcmiwO0lENlogHfuNuCxeW2Tihc6r0zww= MIME-Version: 1.0 In-Reply-To: References: <1311615971.22847.26.camel@selene> From: Ian Kelly Date: Fri, 29 Jul 2011 17:03:15 -0600 Subject: Re: Identical descriptor value, without leaking memory? To: Jack Bates Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 19 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311980625 news.xs4all.nl 23926 [2001:888:2000:d::a6]:35075 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10555 On Fri, Jul 29, 2011 at 5:01 PM, Ian Kelly wrote: > On Mon, Jul 25, 2011 at 11:46 AM, Jack Bates wrote: >> How can you get a descriptor to return an identical value, each time >> it's called with the same "instance" - without leaking memory? Oops, that should have been: class MyDescriptor(object): def __get__(self, instance, owner): try: return instance.__cached_value[self] except AttributeError: instance.__cached_value = {self: get_value(instance, owner)} except KeyError: instance.__cached_value[self] = get_value(instance, owner) return instance.__cached_value[self] class MyClass(object): my_attribute = MyDescriptor()