Recursive inner function... How?

Bjorn Pettersen pbjorn at uswest.net
Sun Dec 31 16:11:11 EST 2000


Ok, this is what I want to do:

    def outer():
        def inner():
            inner()
        inner()

    outer()

i.e. I call outer, outer has an auxiliary sub function called inner that
recurses to infinity (that is not the problem). The above gives the
expected result that inner is not defined inside inner. Inconvenient,
but we know the drill:

    def outer():
        def inner(inner=inner):
            inner()
        inner()

    outer()

however, in 2.0 that gives an "UnboundLocalError: Local variable 'inner'
referenced before assignment". The following works of course, but is
rather repetitive...

    def outer():
        def inner(inner):
               inner(inner)
        inner(inner)

    outer()

Please tell me I'm doing something wrong?

-- bjorn





More information about the Python-list mailing list