By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

Re: Weird local variables behaviors

P: n/a
Sebastjan Trepca wrote:
Hey,

can someone please explain this behavior:

The code:

def test1(value=1):
def inner():
print value
inner()
def test2(value=2):
def inner():
value = value
inner()

test1()
test2()

[trepca@sauron ~/dev/tests]$ python locals.py
1
Traceback (most recent call last):
File "locals.py", line 13, in <module>
test2()
File "locals.py", line 10, in test2
inner()
File "locals.py", line 9, in inner
value = value
UnboundLocalError: local variable 'value' referenced before assignment

Why can't he find the variable in the second case?
Thanks, Sebastjan
Python doesn't like when you read a variable that exists in an outer
scope, then try to assign to it in this scope.

(When you do "a = b", "b" is processed first. In this case, Python
doesn't find a "value" variable in this scope, so it checks the outer
scope, and does find it. But then when it gets to the "a = " part...
well, I don't know, but it doesn't like it.)

In Python 3 (and maybe 2.6), you'll be able to put "nonlocal value" in
inner() to tell it to use the outer scope. (It's similar to the "global"
keyword, but uses the next outer scope as opposed to the global scope.)
--
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Jun 20, 7:32*pm, Matt Nordhoff <mnordh...@mattnordhoff.comwrote:
Sebastjan Trepca wrote:
Hey,
can someone please explain this behavior:
The code:
def test1(value=1):
* * def inner():
* * * * print value
* * inner()
def test2(value=2):
* * def inner():
* * * * value = value
* * inner()
test1()
test2()
[trepca@sauron ~/dev/tests]$ python locals.py
1
Traceback (most recent call last):
* File "locals.py", line 13, in <module>
* * test2()
* File "locals.py", line 10, in test2
* * inner()
* File "locals.py", line 9, in inner
* * value = value
UnboundLocalError: local variable 'value' referenced before assignment
Why can't he find the variable in the second case?
Thanks, Sebastjan

Python doesn't like when you read a variable that exists in an outer
scope, then try to assign to it in this scope.

(When you do "a = b", "b" is processed first. In this case, Python
doesn't find a "value" variable in this scope, so it checks the outer
scope, and does find it. But then when it gets to the "a = " part...
well, I don't know, but it doesn't like it.)
In a language like C++, the scope of a variable is determined by the
declaration.

int x; // A
class Example
{
int x; // B
void f()
{
int x; // C
x = 42; // Which x?
}
};

The "x" referred to in the statement "x = 42;" refers to local
variable of Example::f. If line C were removed, then it would refer
to the member variable of class Example. And if line B were removed,
then it would refer to the global variable.

In Python, however, there are no declarations. Therefore, it requires
another approach. What it chose was:

(1) Explicit "self" for object attributes.
(2) A function's local variables are defined as those appearing on the
left side of an assignment. Whether the name happens to refer to a
global is NOT considered.
Jun 27 '08 #2

P: n/a
I see, intuitively one would think it would try to get it from global
context as it's not yet bound in the local.

Thanks for the explanation.

Sebastjan
On Sat, Jun 21, 2008 at 5:48 AM, Dan Bishop <da*****@yahoo.comwrote:
On Jun 20, 7:32 pm, Matt Nordhoff <mnordh...@mattnordhoff.comwrote:
>Sebastjan Trepca wrote:
Hey,
can someone please explain this behavior:
The code:
def test1(value=1):
def inner():
print value
inner()
def test2(value=2):
def inner():
value = value
inner()
test1()
test2()
[trepca@sauron ~/dev/tests]$ python locals.py
1
Traceback (most recent call last):
File "locals.py", line 13, in <module>
test2()
File "locals.py", line 10, in test2
inner()
File "locals.py", line 9, in inner
value = value
UnboundLocalError: local variable 'value' referenced before assignment
Why can't he find the variable in the second case?
Thanks, Sebastjan

Python doesn't like when you read a variable that exists in an outer
scope, then try to assign to it in this scope.

(When you do "a = b", "b" is processed first. In this case, Python
doesn't find a "value" variable in this scope, so it checks the outer
scope, and does find it. But then when it gets to the "a = " part...
well, I don't know, but it doesn't like it.)

In a language like C++, the scope of a variable is determined by the
declaration.

int x; // A
class Example
{
int x; // B
void f()
{
int x; // C
x = 42; // Which x?
}
};

The "x" referred to in the statement "x = 42;" refers to local
variable of Example::f. If line C were removed, then it would refer
to the member variable of class Example. And if line B were removed,
then it would refer to the global variable.

In Python, however, there are no declarations. Therefore, it requires
another approach. What it chose was:

(1) Explicit "self" for object attributes.
(2) A function's local variables are defined as those appearing on the
left side of an assignment. Whether the name happens to refer to a
global is NOT considered.
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.