473,799 Members | 2,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unbound Local error? How?

I've got some code as follows:

import re
re_regexname = re.compile('abc ')

......
...... various function defs
......

def func1():
...
func2()
...

def func2():
if re_regexname.ma tch('abc'):
<do something>

if __name__ == '__main__':
func1()
but this returns the Traceback:

UnboundLocalErr or: local variable 're_regexname' referenced before
assignment
How?

It was declared in the zero level indentation near the top of the
script! I don't understand this, isn't a function supposed to be able to
reference stuff in the containing function or the top level?
Jun 27 '06 #1
3 2986
Hari Sekhon wrote:
import re
re_regexname = re.compile('abc ')

.....
..... various function defs
.....

def func1():
...
func2()
...

def func2():
if re_regexname.ma tch('abc'):
<do something>

if __name__ == '__main__':
func1()

The above clearly is not what you have. See the attached version of the
above that works. So - go check for a typo or something like that.

Diez
Jun 27 '06 #2
Hari Sekhon wrote:
I've got some code as follows:

import re
re_regexname = re.compile('abc ')

.....
..... various function defs
.....

def func1():
...
func2()
...

def func2():
if re_regexname.ma tch('abc'):
<do something>

if __name__ == '__main__':
func1()
but this returns the Traceback:

UnboundLocalErr or: local variable 're_regexname' referenced before
assignment
this is *not* the traceback. This is only the error message. The
traceback contains all needed informations (or at least all possible
information at this point) to know what happened. But you did not post
the traceback. Nor did you post the minimal runnable code snippet
producing this error.

How?


How could we know ?
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom. gro'.split('@')])"
Jun 27 '06 #3
Hari Sekhon wrote:
Bruno Desthuilliers wrote:
Hari Sekhon wrote:

I've got some code as follows:

import re
re_regexna me = re.compile('abc ')

.....
..... various function defs
.....

def func1():
...
func2()
...

def func2():
if re_regexname.ma tch('abc'):
<do something>

if __name__ == '__main__':
func1()
but this returns the Traceback:

UnboundLocal Error: local variable 're_regexname' referenced before
assignment


this is *not* the traceback. This is only the error message. The
traceback contains all needed informations (or at least all possible
information at this point) to know what happened. But you did not post
the traceback. Nor did you post the minimal runnable code snippet
producing this error.
How?


How could we know ?


sorry, I know it looks like I was being stingy but the traceback was not
that helpful, not without seeing more a huge amount more of the code. I
was trying to abbreviate.

Traceback (most recent call last):
File "./backup.py", line 649, in ?
backup(machine, share)
File "./backup.py", line 364, in backup
backupdir(sourc e,destination)
File "./backup.py", line 398, in backupdir
(dirlist,fileli st) = getdirlisting( source )
File "./backup.py", line 445, in getdirlisting
if re_skip_dirs.ma tch(x):
UnboundLocalErr or: local variable 're_skip_dirs' referenced before
assignment

This doesn't really show that much, I figured the problem was the following:

def getdirlisting() :
re_skip_dirs = re_skip_top_dir s #Here's the culprit

where both these regex compiled objects were declared at the top level,
it seems that the assignment is trying to use the local variable
re_skip_top_dir s which doesn't exist, that's why I'm getting a
traceback, commenting out this line it runs fine.

-h

The error is simply that you are making an assignment *somewhere* inside
your function body, so the compiler is treating the variable as local,
masking the module-level global name. Consequently when you try to read
its value you are told that the local variable has not yet been bound to
a value.

A "global" statement inside the function body will fix the problem. You
could also add a keyword argument (never used except to set a default
for it in the "def" statement).

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Jun 27 '06 #4

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

Similar topics

3
6343
by: Pat | last post by:
Hello, I've used Sum() to total bound fields on a continuous form with no problem. However, I now have a continuous form, on which I use an unbound field to calculate the number of hours between two times stored in a record (bound fields). This field is called txtDuration. On my footer, I've created another unbound field named txtWeeklyTotal. I've set the Control Source to =Sum(), but this returns a #Name? error. I've tried . and it...
4
2194
by: Lumpierbritches | last post by:
Thank you once again for any and all assistance. I'm building an application that's getting quite bulky due to the number of forms, macros and procedures. I was wondering if there's a way to use 1 (one) unbound form, if all the fields are the same for multiple forms populating them with Queries? I have a makeshif menu form with command buttons and I would like to eliminate all the forms and replace them with one unbound form for three...
2
3893
by: Zlatko Matiæ | last post by:
Hello. I have the following problem with MS Access/PostgreSQL combination: There is a form in Access that has an unbound text box, used for entering a commentary of a batch of records. There is a DAO Append Query that has a parameter that is passed from the text box using parameter of DAO QueryDef object. I adjusted B7 parameter (Text as LongVarchar) in connection string (ODBC driver) to 1, so that Access links PostgreSQL Text fields as...
7
3645
by: Rainer Queck | last post by:
Hi NG, Why is it, that in my DataGridView all columns which are not bound to a "DataProperty" loos thier content in a couple of occasions? This happens if: - Underlaying DataSet.AcceptChanges() - DataGridView bound columns -Click on header for sorting Is there a event, that I can use to detect this situation and then
2
13939
by: void.no.spam.com | last post by:
I'm a novice at Python, and found some code samples on how to use threads. My script is being run by a product that contains a Jython interpreter. Can someone please explain why I get the following error: Traceback (innermost last): File "/full/path/to/file/GenerateData.py", line 104, in ? File "/full/path/to/file/GenerateData.py", line 34, in __init__ TypeError: unbound method must be called with class instance 1st argument
14
3040
by: 7stud | last post by:
Here is some example code that produces an error: class Test(object): def greet(): print "Hello" t = Test() t.greet() TypeError: greet() takes no arguments (1 given)
2
1412
by: righes | last post by:
hi folks, suppose this snipplet: spam = 42 def eggs(): print spam spam = spam + 1
6
2691
by: Volker Neurath | last post by:
Hi all, I have a Problem with combobox-property "NotInList" and an unbound Form. The situation: On my main form i have three comboboxes for data-exchange (here: Names of distributor, reseller and final customers, the whole database is made for storing information about quotatations - no, not for quoting itself) ut the boxes actually may not contain all our distributors and reseller's
4
4036
by: sumit kale | last post by:
Hi, Can somebody help me resolve my problem ? I am getting error when calculating total using unbound textfiled in subform. I have a main form called purchase_register_master and a subform called purchase_register_details, In sub form i have a unbound textfield called txt_Total_Amount where in i am calculating total amount for different items purchased by giving following code in control source. ContolSource: =Sum() In my main form i...
0
9687
marktang
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...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10485
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, 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...
0
10252
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10231
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,...
1
7565
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 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...
0
6805
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.