Where do nested functions live?

Diez B. Roggisch deets at nospam.web.de
Sat Oct 28 12:32:37 EDT 2006


> If I may turn the issue around, I could see a need for an inner function 
> to be able to access the variables of the outer function, the same way a 
> function can access globals. Why? Because inner functions serve to 
> de-multiply code segments one would otherwise need to repeat or to 
> provide a code segment with a name suggestive of its function. In either 
> case the code segment moved to the inner function loses contact with its 
> environment, which rather mitigates its benefit.

Maybe I'm dense here, but where is your point? Python has nested lexical 
scoping, and while some people complain about it's actual semantics, it 
works very well:

def outer():
    outer_var = 10
    def inner():
        return outer_var * 20
    return inner

print outer()()



Diez



More information about the Python-list mailing list