One-Shot Property?

Dave Benjamin ramen at
Tue Jan 18 12:53:43 EST 2005

Kevin Smith wrote:
> I have many cases in my code where I use a property for calculating a 
> value on-demand.  Quite a few of these only need to be called once.  
> After that the value is always the same.  In these properties, I set a 
> variable in the instance as a cached value and return that value on 
> subsequent calls.  It would be nice if there was a descriptor that would 
> do this automatically.  Actually, what would be really nice is if I 
> could replace the property altogether and put the calculated value in 
> its place after the first call, but the property itself prevents me from 
> doing that.  Is this possible?

I was going to recommend taking a look at the "memoize" example on the 
Python wiki, but it seems to be missing at the moment. In any case, 
here's the URL:

There are also a few examples on the Cookbook, like this one:

It shouldn't be too difficult to adapt this technique so that it can be 
used to create properties. I wouldn't bother replacing the property with 
an attribute unless you have a specific reason to do so (performance, 


More information about the Python-list mailing list