473,288 Members | 2,350 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

Re: Weird local variables behaviors

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
2 1505
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Catherine Lynn Wood | last post by:
I have a page that I just developed using a combination of stylesheets and div layers. It uses a 'tab' style system placing four div layers in the same space with visibility 'hidden' and position...
5
by: news | last post by:
I've a site: http://gto.ie-studios.net/products.php that looks perfectly fine in Windows whether with IE or Firefox 1.0. But when viewed in the Linux version of Firefox 1.0, images get misaligned...
82
by: nobody | last post by:
Howdy, Mike! mikecoxlinux@yahoo.com (Mike Cox) wrote in message news:<3d6111f1.0402271647.c20aea3@posting.google.com>... > I'm a C++ programmer, and have to use lisp because I want to use >...
5
by: Tommytrojan | last post by:
Hi, I have been using Python for a while but today I came across a really strange behavior: While poking around in Queue.py due to problems with importing this module from a thread I got an...
2
by: Nils Emil P. Larsen | last post by:
Hello I have read about a C shared library which I want to use in my C program. (It's a library to encode/decode packets from/to a serial bus running with the SNAP-protocol). Unfortunatly...
3
by: Nick Goloborodko | last post by:
Hi, I've been having a really weird problem with required field validator. Here's the detailed description of the problem: I have originally been developing my web application on Windows XP...
0
by: juxstapose | last post by:
Hello, I have been develop a blocking socket application with threading. The main thread handles connections and inserts them into python's protected queue as jobs for the thread pool to handle....
1
by: Mark Denardo | last post by:
I recently upgraded to VS2005 and converted some projects from 1.1 to 2.0 and am seeing two weird behaviors I can't seem to resolve, and am wondering if they are bugs or something I'm forgetting to...
0
by: Sebastjan Trepca | last post by:
Hey, can someone please explain this behavior: The code: def test1(value=1): def inner(): print value inner()
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.