On Tue, Apr 12, 2011 at 10:27 AM, James Mills <prolo...@shortcircuit.net.au> wrote: > This could be simplified to just: > > return expr or None > > And more to the point... If your calee is relying > on the result of this function, just returning the > evaluation of "expr" is enough.
I'm thinking here that that's not a solution; he'll have more code to follow. An example of what I think he's trying to do: 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 If the rest of the function can be implemented as an expression, it might be possible to use: return expr or other_expr But in the example of a lookup cache, that wouldn't work so easily - assignment isn't an expression. If 'x=y' had a value as it does in C, the above function could become: def fac(n): return cache[n] or (cache[n]=1 if n<=1 else fac(n-1)*n) which is a reasonable one-liner, albeit not the most efficient factorial implementation. Is there a simple and Pythonic way to do this? BTW, assume for the purposes of discussion that the return? expr is a complex one, such that it's well worth evaluating only once (maybe even has side effects). Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list