Mathematica has a convenient Nest function for repeatedly evaluating f(f(f(f(...f(n))))). Not something you need every day, but occasionally useful. Here is a trivial implementation:
def nest(f, expr, n):
assert n >= 0
if n == 0:
return expr
else:
return f(nest(f, expr, n - 1))
>>> nest(lambda x: (1 + x) ** 2, 1, 3)
676
Is there a Pythonic way to do this?