Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #10554

Re: Identical descriptor value, without leaking memory?

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsreader4.netcologne.de!news.netcologne.de!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'value,': 0.04; 'instance': 0.05; 'instance,': 0.05; 'attribute': 0.09; 'attribute.': 0.09; 'descriptor': 0.09; 'descriptors': 0.09; '25,': 0.12; 'am,': 0.13; 'wrote:': 0.15; 'subject:memory': 0.16; 'object,': 0.16; 'cc:addr :python-list': 0.16; 'mon,': 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; 'unlikely': 0.23; 'cache': 0.25; 'skip:_ 20': 0.28; 'message- id:@mail.gmail.com': 0.28; 'object': 0.30; 'cc:addr:python.org': 0.30; 'class': 0.31; 'subject:?': 0.31; "won't": 0.32; 'received:209.85.161.46': 0.34; 'received:mail- fx0-f46.google.com': 0.34; 'identical': 0.35; 'try:': 0.35; 'conflict': 0.37; 'received:google.com': 0.38; 'received:209.85.161': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'case': 0.39; 'except': 0.39; 'received:209': 0.40; 'called': 0.40; 'full': 0.63; 'placing': 0.74; '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=kJarn84tRqLaVb24k9pU+f1DfYOwyMr8lO3aCQSmjRY=; b=Ku0urW4ZiNtkS8VKksccX9TEi6NMp48nFZWsSUVylTYQ4MXLdSshjsZxIKyVpACeVM uHO2cdQd0BkYx556G3DCB3pYMdTyveSX2wirnDuLpLLE9j3YkKTS3KqPgcJpEisrNRS3 D/pyzg2C7BFq2Loxx+DeluzdfI5Aii4mpgTWA=
MIME-Version 1.0
In-Reply-To <1311615971.22847.26.camel@selene>
References <1311615971.22847.26.camel@selene>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 29 Jul 2011 17:01:04 -0600
Subject Re: Identical descriptor value, without leaking memory?
To Jack Bates <ms419@freezone.co.uk>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1642.1311980495.1164.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1311980495 news.xs4all.nl 23876 [2001:888:2000:d::a6]:33678
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10554

Show key headers only | View raw


On Mon, Jul 25, 2011 at 11:46 AM, Jack Bates <ms419@freezone.co.uk> wrote:
> How can you get a descriptor to return an identical value, each time
> it's called with the same "instance" - without leaking memory?

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

class MyClass(object):
    my_attribute = MyDescriptor()


Placing the cache on the object instance ensures that it won't outlive
the object, and since the full attribute name will be
_MyDescriptor__cached_value, it's highly unlikely to conflict with an
existing attribute.  Keying the value on self is necessary in case the
object has multiple descriptors of the same type.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Identical descriptor value, without leaking memory? Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-29 17:01 -0600

csiph-web