Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #10555 > unrolled thread
| Started by | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| First post | 2011-07-29 17:03 -0600 |
| Last post | 2011-07-29 17:03 -0600 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Identical descriptor value, without leaking memory? Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-29 17:03 -0600
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-07-29 17:03 -0600 |
| Subject | Re: Identical descriptor value, without leaking memory? |
| Message-ID | <mailman.1643.1311980625.1164.python-list@python.org> |
On Fri, Jul 29, 2011 at 5:01 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> 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?
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()
Back to top | Article view | comp.lang.python
csiph-web