Background: In Python, I'm trying writing a generator function that yields functions that are repeated applications of a one-argument function f. The first function yielded should apply f 0 times (the identity function), the second function yielded should apply f once, etc. The function's doctest string is in the following.
def repeated(f):
    """
    >>> double = lambda x: 2 * x
    >>> funcs = repeated(double)
    >>> identity = next(funcs)
    >>> double = next(funcs)
    >>> quad = next(funcs)
    >>> oct = next(funcs)
    >>> quad(1)
    4
    >>> oct(1)
    8
    >>> [g(1) for _, g in
    ...  zip(range(5), repeated(lambda x: 2 * x))]
    [1, 2, 4, 8, 16]
    """
    
def repeated(f):
    g = lambda x: x
    while 1:
        yield g
        g = lambda x: f(g(x))
Problem: In the code above, max recursion depth error on line "g = lambda x: f(g(x))". But when I change that line to "g = (lambda h: lambda x: f(h(x)))(g)", it works. Why does this happen? What's the difference between these two statements?
def repeated(f):
    g = lambda x: x
    while 1:
        yield lambda x: g(x)
        g = (lambda h: lambda x: f(h(x)))(g)
Extra Problem:The code block above almost passes the doctest, but still fails. Failed example: quad(1) Expected: 4 Got: 8
However, when I replace the "yield lambda x: g(x)" to "yield g", it passes the doctest. What's the difference between "lambda x: g(x)" and "g"?
 
    