free and nonlocal variables

B

bartolome.sintes

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"?

Thanking you in advance,
Bartolomé Sintes
 
8

88888 Dihedral

(e-mail address removed)æ–¼ 2013å¹´3月21日星期四UTC+8下åˆ4時52分17秒寫é“:
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"?



Thanking you in advance,

Bartolomé Sintes

In python the interpreter has to check 4 levels of dictionaries
in the run time to perform an action. The for levels are:
1. object instance level
2. class level
3. local function level
4. global level.

Objects created at level 1,2,3 can be returned to some other
object in the run time.

Thus a GC is available to save the trouble of tracking
everything for the programmer in a complex system.
 
S

Steven D'Aprano

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.
 
T

Terry Reedy

In Python 3, "free variable" and "nonlocal variable" are synonym terms?

Yes, but that is idiosyncratic to Python.
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"?

I believe that is the usual definition, such as you would find on Wikipedia.
 
N

Nobody

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.
 
J

Jussi Piitulainen

Nobody said:
"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.

And in (lambda x : x)(x) + x below, x occurs both free and bound. The
free occurrences are bound by the outer lambda.
6
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,269
Messages
2,571,100
Members
48,773
Latest member
Kaybee

Latest Threads

Top