free and nonlocal variables

Steven D'Aprano steve+comp.lang.python at
Thu Mar 21 12:00:16 CET 2013

On Thu, 21 Mar 2013 01:52:17 -0700, bartolome.sintes wrote:

> In Python 3, "free variable" and "nonlocal variable" are synonym terms?
> Or is there a difference, like "a free variable is a variable that is
> not a local variable, then nonlocal variables and global variables are
> both free variables"?

"Free variable" is a formal term from computer science. As far as I know, 
Python uses it in exactly the same way.

A free variable is a variable in an expression or function that is not 
local (which includes function parameters) to that expression. So both 
global and non-local variables are free variables.

def spam(x):
    def inner():
        y = x**2 - 1
        return x + y + z
    return inner()

In inner(), both x and z are free variables, but y is not, since it is 
local to the inner() function.


More information about the Python-list mailing list