In article <[email protected]>,
D'Arcy J.M. Cain <[email protected]> wrote:
>On Sun, 17 Apr 2011 16:21:53 +1200
>Gregory Ewing <[email protected]> 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]
Aside from Steven's entirely appropriate pointed question, I find this
more readable:
if x not in cache:
Without testing, I'm not sure, but I believe it's more efficient, too
(creates fewer bytecodes).
--
Aahz ([email protected]) <*> http://www.pythoncraft.com/
"At Resolver we've found it useful to short-circuit any doubt and just
refer to comments in code as 'lies'. :-)"
--Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22
--
http://mail.python.org/mailman/listinfo/python-list