Feature suggestion -- return if true

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Apr 17 11:10:47 EDT 2011


On Sun, 17 Apr 2011 08:33:47 -0400, D'Arcy J.M. Cain wrote:

> On Sun, 17 Apr 2011 16:21:53 +1200
> Gregory Ewing <greg.ewing at canterbury.ac.nz> wrote:
>> My idiom for fetching from a cache looks like this:
>> 
>>    def get_from_cache(x):
>>      y = cache.get(x)
>>      if not y:
>>        y = compute_from(x)
>>        cache[x] = y
>>      return y
> 
> I prefer not to create and destroy objects needlessly.
> 
>  def get_from_cache(x):
>    if not x in cache:
>      cache[x] = compute_from(x)
>    return cache[x]

What object(s) do you think are being created and destroyed needlessly?

(This is not a rhetorical question.)


-- 
Steven



More information about the Python-list mailing list