Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41638 > unrolled thread

free and nonlocal variables

Started bybartolome.sintes@gmail.com
First post2013-03-21 01:52 -0700
Last post2013-03-21 22:36 +0200
Articles 6 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  free and nonlocal variables bartolome.sintes@gmail.com - 2013-03-21 01:52 -0700
    Re: free and nonlocal variables 88888 Dihedral <dihedral88888@googlemail.com> - 2013-03-21 02:34 -0700
    Re: free and nonlocal variables Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-21 11:00 +0000
    Re: free and nonlocal variables Terry Reedy <tjreedy@udel.edu> - 2013-03-21 07:55 -0400
    Re: free and nonlocal variables Nobody <nobody@nowhere.com> - 2013-03-21 19:05 +0000
      Re: free and nonlocal variables Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-03-21 22:36 +0200

#41638 — free and nonlocal variables

Frombartolome.sintes@gmail.com
Date2013-03-21 01:52 -0700
Subjectfree and nonlocal variables
Message-ID<263e910f-4cc5-4b50-bf8d-c207622170b5@googlegroups.com>
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

[toc] | [next] | [standalone]


#41640

From88888 Dihedral <dihedral88888@googlemail.com>
Date2013-03-21 02:34 -0700
Message-ID<7f0df9d6-6370-478a-85fb-6e6f2ed869c5@googlegroups.com>
In reply to#41638
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.

[toc] | [prev] | [next] | [standalone]


#41643

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-03-21 11:00 +0000
Message-ID<514ae83f$0$30001$c3e8da3$5496439d@news.astraweb.com>
In reply to#41638
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

[toc] | [prev] | [next] | [standalone]


#41645

FromTerry Reedy <tjreedy@udel.edu>
Date2013-03-21 07:55 -0400
Message-ID<mailman.3584.1363866922.2939.python-list@python.org>
In reply to#41638
On 3/21/2013 4:52 AM, bartolome.sintes@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

[toc] | [prev] | [next] | [standalone]


#41658

FromNobody <nobody@nowhere.com>
Date2013-03-21 19:05 +0000
Message-ID<pan.2013.03.21.19.05.37.752000@nowhere.com>
In reply to#41638
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.

[toc] | [prev] | [next] | [standalone]


#41667

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2013-03-21 22:36 +0200
Message-ID<qothak4h4na.fsf@ruuvi.it.helsinki.fi>
In reply to#41658
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web