free and nonlocal variables

2013-03-21 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: free and nonlocal variables

2013-03-21 Thread 88888 Dihedral
bartolom...@gmail.com於 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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: free and nonlocal variables

2013-03-21 Thread Steven D'Aprano
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.



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: free and nonlocal variables

2013-03-21 Thread Terry Reedy

On 3/21/2013 4:52 AM, bartolome.sin...@gmail.com wrote:

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.

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: free and nonlocal variables

2013-03-21 Thread Nobody
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


Re: free and nonlocal variables

2013-03-21 Thread Jussi Piitulainen
Nobody writes:
 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.

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

(lambda x : (lambda x : x)(x) + x)(3)
   6
-- 
http://mail.python.org/mailman/listinfo/python-list