Converting functions

Edmunds Cers edmunds at laivas.lv
Mon Jan 24 06:12:17 EST 2011


Peter Otten <__peter__ at web.de> writes:

> I don't know how to express it more clearly, so here's another example:
>
>>>> def f():
> ...     def g(): return a * a
> ...     def h(): return a + a
> ...     a = 5
> ...     return g, h
> ...
>>>> g, h = f()
>>>> g(), h()
> (25, 10)

IMHO this whole confusion just shows that mingling assignment and
binding makes understanding scope harder. In Python, the first
assignment inside a function body also creates a binding (unless told
not to do so by global) the scope of which is the _whole_ of the
function body. A variable reference refers to the lexically innermost
surrounding binding of said variable. Now, while it might seem that some
magic happens in the example on the return of the function, this is in
fact not so, since the assignment "a = 5" actually creates a binding for
/a/ that is visible from the body of /g/, because the lexical scope of
the binding is the whole body of /f/, so that the capture of the
variable happens inside of the def expression (as one would expect) and
not on return as you seem to imply.

Slightly OT -- the above also explains why closed over variables are
read only in Python. An assignment inside a closure would implicitly
create a binding, so that all (even previous) references to that
variable would refer to this new binding.

> I think this behaviour is also called "late binding".

"Retroactive implicit scope" would be closer.

-- 
A change in perspective is worth 80 IQ points. --- Alan Kay



More information about the Python-list mailing list