Identical descriptor value, without leaking memory?

Ian Kelly ian.g.kelly at
Sat Jul 30 01:03:15 CEST 2011

On Fri, Jul 29, 2011 at 5:01 PM, Ian Kelly <ian.g.kelly at> wrote:
> On Mon, Jul 25, 2011 at 11:46 AM, Jack Bates <ms419 at> 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):
            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()

More information about the Python-list mailing list