Chris Angelico wrote:

def fac(n):
    # attempt to get from a cache
    return? cache[n]
    # not in cache, calculate the value
    ret=1 if n<=1 else fac(n-1)*n
    # and cache and return it
    cache[n]=ret; return ret

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

which doesn't require any conditional returns.

--
Greg
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to