473,466 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Curious UnboundLocalError Behavior

I'm probably fundamentally misunderstanding the way the interpreter
works with regard to scope, but is this the intended behavior...

franz-macbook:~ mdfranz$ python unboundlocal.py
('Darwin', 'franz-macbook.local', '8.8.5', 'Darwin Kernel Version
8.8.5: Mon Dec 11 19:39:17 PST 2006;
root:xnu-792.16.5.obj~1/RELEASE_I386', 'i386')
2.4.3 (#1, Feb 24 2007, 23:01:32)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]
{'__builtins__': <module '__builtin__' (built-in)>, '__file__':
'unboundlocal.py', 'SOMEGLOBAL': 1, 'sys': <module 'sys' (built-in)>,
'__name__': '__main__', 'foo': <function foo at 0x42cf0>, 'os':
<module 'os' from
'/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/os.pyc'>,
'__doc__': None}
SOMEGLOBAL:
Traceback (most recent call last):
File "unboundlocal.py", line 15, in ?
foo()
File "unboundlocal.py", line 11, in foo
print "SOMEGLOBAL:",SOMEGLOBAL
UnboundLocalError: local variable 'SOMEGLOBAL' referenced before assignment
Where unboundlocal.py is...

import os,sys

SOMEGLOBAL=1

def foo():
dome=False
if dome:
SOMEGLOBAL = 0

print globals()
print "SOMEGLOBAL:",SOMEGLOBAL

print os.uname()
print sys.version
foo()

Is SOMEGLOBAL is some weird in-between state, since it is referenced
within foo() but not actually assigned?

If I set dome to True SOMEGLOBAL gets overriden (as I would have expected)

franz-macbook:~ mdfranz$ python unboundlocal.py
('Darwin', 'franz-macbook.local', '8.8.5', 'Darwin Kernel Version
8.8.5: Mon Dec 11 19:39:17 PST 2006;
root:xnu-792.16.5.obj~1/RELEASE_I386', 'i386')
2.4.3 (#1, Feb 24 2007, 23:01:32)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)]
{'__builtins__': <module '__builtin__' (built-in)>, '__file__':
'unboundlocal.py', 'SOMEGLOBAL': 1, 'sys': <module 'sys' (built-in)>,
'__name__': '__main__', 'foo': <function foo at 0x42cf0>, 'os':
<module 'os' from
'/opt/local/Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/os.pyc'>,
'__doc__': None}
SOMEGLOBAL: 0

--
Matthew Franz
http://www.threatmind.net/
Feb 28 '07 #1
2 1503
On 28 fév, 18:15, "Matthew Franz" <mdfr...@gmail.comwrote:
I'm probably fundamentally misunderstanding the way the interpreter
works with regard to scope, but is this the intended behavior...
(snip traceback)
import os,sys

SOMEGLOBAL=1

def foo():
dome=False
if dome:
SOMEGLOBAL = 0

This makes SOMEGLOBAL local !-)

Look for the 'global' statement. Or better, try to avoid rebinding
globals from within a function.

As as side note: by convention, ALL_UPPER names denote constants.

Mar 1 '07 #2
I had tried the global prefix in the real code (vs. the contrived
example in the post), but in the incorrect code block, which made me
think something else was up. Yes, these were supposed to be constants
that under rare circumstances were changed ;) In the end, I scrapped
the rebind approach, because that wasn't the behavior I wanted
either.Thanks for the help.

- mdf

On 1 Mar 2007 00:20:05 -0800, br*****************@gmail.com
<br*****************@gmail.comwrote:
On 28 fév, 18:15, "Matthew Franz" <mdfr...@gmail.comwrote:
I'm probably fundamentally misunderstanding the way the interpreter
works with regard to scope, but is this the intended behavior...
(snip traceback)
import os,sys

SOMEGLOBAL=1

def foo():
dome=False
if dome:
SOMEGLOBAL = 0


This makes SOMEGLOBAL local !-)

Look for the 'global' statement. Or better, try to avoid rebinding
globals from within a function.

As as side note: by convention, ALL_UPPER names denote constants.

--
http://mail.python.org/mailman/listinfo/python-list

--
Matthew Franz
http://www.threatmind.net/
Mar 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brad Clements | last post by:
I was going to file this as a bug in the tracker, but maybe it's not really a bug. Poor Python code does what I consider to be unexpected. What's your opinion? With Python 2.3.2 (but also...
6
by: Alex Gittens | last post by:
I'm trying to define a function that prints fields of given widths with specified alignments; to do so, I wrote some helper functions nested inside of the print function itself. I'm getting an...
2
by: silverburgh.meryl | last post by:
Can you please tell me what is the meaning this error in general? UnboundLocalError: local variable 'colorIndex' referenced before assignment In my python script, I have a variable define and...
8
by: David Bear | last post by:
I'm attempting to use the cgi module with code like this: import cgi fo = cgi.FieldStorage() # form field names are in the form if 'name:part' keys = fo.keys() for i in keys: try:...
15
by: Paddy | last post by:
Hi, I am trying to work out why I get UnboundLocalError when accessing an int from a function where the int is at the global scope, without explicitly declaring it as global but not when accessing...
9
by: Camellia | last post by:
hi all why it generates an "UnboundLocalError" when I do the following: <code> .... def main(): number = number() number_user = user_guess() while number_user != number:
2
by: Konstantinos Pachopoulos | last post by:
Hi, i have a problem, the source of which is probably the fact, that i have not understood how to declare global variables - I use the Jython compiler, but i think this is a Python issue... ...
8
by: defn noob | last post by:
isPrime works when just calling a nbr but not when iterating on a list, why? adding x=1 makes it work though but why do I have to add it? Is there a cleaner way to do it? def isPrime(nbr):...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
0
tracyyun
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...
0
isladogs
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.