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
UnboundLocalErr or: 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.)
-- 2 1528
On Jun 20, 7:32*pm, Matt Nordhoff <mnordh...@matt nordhoff.comwro te:
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
UnboundLocalErr or: 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.
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...@matt nordhoff.comwro te:
>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
UnboundLocalErr or: 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 absolute. They are inside
of another parent div container with position set relative so that as I make
the objects visible, I can also set their position to relative so they
redraw the container size. The parent div is inside of a table cell...
|
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 and breaks between then appear.
Why the two different behaviors on the same browser but different OS's?
Anyway, that page as an example DOES W3C HTML validate AND W3C CSS
validate as well.
So, there's no ERRORS per se, but I suppose there's...
|
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
> emacs. I've gotten a book on lisp, and I must say lisp is the ugliest
> looking language syntax wise. What is up with this: (defun(foo()).
(DEFUN FOO () NIL)
> What were the lisp authors thinking? Why did Stallman use lisp in
|
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 error that a module that I imported on
top of the file could not be accessed. I reduced the problem to this
small script:
|
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 there is no source code available, just a C file with
function declarations like:
long SendData(TXData)Type_TXData *TXData;
| |
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 SP2
under IIS5 with .NET framework V1.1.4322. I have had a couple of
RequiredFieldValidator, to ensure that the user enter the username and
password on the login form and avoid unnessesary postbacks.
|
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.
There is not much information on threading.local except that it states
that in maintains variable uniqueness among multiple instances of the
same thread. I am assuming that it implements some sort of locking
functionality under the covers? ...
|
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 do or add.
(issue 1)
When setting up a panel control with border settings:
<asp:Panel Backcolor="BurlyWood" Borderstyle="Ridge"
BorderColor="Black" BorderWidth="1px" ... Runat="server" />
|
by: Sebastjan Trepca |
last post by:
Hey,
can someone please explain this behavior:
The code:
def test1(value=1):
def inner():
print value
inner()
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |