Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41658
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Subject | Re: free and nonlocal variables |
| Date | 2013-03-21 19:05 +0000 |
| Message-Id | <pan.2013.03.21.19.05.37.752000@nowhere.com> |
| Newsgroups | comp.lang.python |
| References | <263e910f-4cc5-4b50-bf8d-c207622170b5@googlegroups.com> |
| Organization | Zen Internet |
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.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web