On Thu, 21 Mar 2013 01:52:17 -0700, bartolome.sintes wrote: > In Python 3, "free variable" and "nonlocal variable" are synonym terms?
"Free variable" is a computer science term. A variable is free if it is not bound. E.g. x and y are free in "x+y", x is bound and y is free in "lambda x: x+y", x and y are both bound in "lambda y: lambda x: x+y". IOW, a variable is free in an expression if the expression doesn't include whatever created the variable. In Python 3, the "nonlocal" keyword indicates that a name refers to a variable created in an outer function. Names are deduced as referring to local, nonlocal (outer) or global variables at compile time. If a name is a function parameter, then it's a local variable. If a function definition doesn't include an assignment to a name, or a global or nonlocal statement for that name, the name refers to a nonlocal variable (local variable in an enclosing function) if one exists, otherwise to a global variable. By default, the presence of an assignment causes the name to be treated as a local variable. If the variable is read prior to assignment, an UnboundLocalError is raised (even if a global or nonlocal variable exists with that name; the decision is made when the function is compiled, not when the assignment is executed). However, a "global" statement causes the name to be treated as a global variable, while a "nonlocal" statement causes it to be treated as a reference to a local variable of the enclosing function. Again, it is the presence of these statements during compilation, not execution of them at run time, which causes the name to be deduced as a global or nonlocal variable. -- http://mail.python.org/mailman/listinfo/python-list