Feature suggestion -- return if true

D'Arcy J.M. Cain darcy at druid.net
Sun Apr 17 08:33:47 EDT 2011


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]

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list