Feature suggestion -- return if true

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Apr 17 19:09:29 EDT 2011


D'Arcy J.M. Cain wrote:
> On 2011年4月17日 16:21:53 +1200
> Gregory Ewing <greg.ewing at canterbury.ac.nz> wrote:
>>> 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.

How does that create objects needlessly?
> def get_from_cache(x):
> if not x in cache:
> cache[x] = compute_from(x)
> return cache[x]

That looks up the cache *twice* for every access. Mine
only does one lookup when the item is already in the
cache.
-- 
Greg


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /