473,385 Members | 1,630 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,385 software developers and data experts.

Binding frustration

So I thought I had come to peace with binding in Python, but then this
happened to me:

I was trying to do things the Python way (as opposed to the Scheme way)
as was suggested to me, and so here's a shortened version of my program:

def getGenres(title): #it takes a movie title and returns a list of
genres that the movie falls into

result = [] # my accumulator

def inGenre(g): # g is a genre
if <here I test if "title" is of genre g (using a simple Python
dictionary I have collected from a mini web crawl)>:
result = result + [g] # if title is of genre g, then add
it to the accumulator

# and then I do a number of inGenre tests:
inGenre('Comedy')
inGenre('Sci-fi')
inGenre('Suspense')
...
return result

So what's wrong with this program? Well, Python tells me:

UnboundLocalError: local variable 'result' referenced before assignment

It seems my only choice is to move result to the global environment,
but if that's not a kludge, I don't know what is. So why doesn't this
work? Python lambdas are able to use "free" variables in this way.
Why not a def? And more importantly, how should I get around this?

Thanks all,

Rob
Jul 18 '05 #1
2 1458
JCM
Rob Hunter <ro*@cs.brown.edu> wrote:
....
def getGenres(title): #it takes a movie title and returns a list of
genres that the movie falls into result = [] # my accumulator def inGenre(g): # g is a genre
if <here I test if "title" is of genre g (using a simple Python
dictionary I have collected from a mini web crawl)>:
result = result + [g] # if title is of genre g, then add


Here you're attempting to rebind a variable which lives in an
enclosing scope. This isn't possible in Python (except for globals).
Use result.append(g) instead and it should work.
Jul 18 '05 #2

"Rob Hunter" <ro*@cs.brown.edu> wrote in message
news:ma**********************************@python.o rg...
def inGenre(g): # g is a genre
if <here I test if "title" is of genre g (using a simple Python dictionary I have collected from a mini web crawl)>:
result = result + [g] # if title is of genre g, then add it to the accumulator

UnboundLocalError: local variable 'result' referenced before

assignment

When, within a function, you assign to a variable that has not been
declared global, then you implicitly declare that variable to be local
to the function -- in this case, inGenre(). But local var 'result'
has not previously been assigned a value within inGenre. Hence the
error message. As JCM said, try result.append(g).

Terry J. Reedy
Jul 18 '05 #3

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

Similar topics

10
by: Frog | last post by:
Hi, i'm not a programmer so I have a very stupid question. I'm trying to make a practical script. I need to run an executable program in it but i can't get it to work. Maybe someone here can...
0
by: Ann Morris | last post by:
INTRODUCTION One of the most powerful aspects of .NET and Windows Forms is data binding. Data binding is the process of associating user interface (UI) elements with a data source to generate a...
9
by: Zlatko Matić | last post by:
I was reading about late binding, but I'm not completely sure what is to be done in order to adjust code to late binding... For example, I'm not sure if this is correct: early binding: Dim ws...
1
by: Bruce | last post by:
Hi, there, I meet a problem about comboBox binding. -------------------- Database: Northwind Tables: 1) Products 2) Categories I create a form (named "form1") to edit the record from...
0
by: JSantora | last post by:
Essentially, InsertAT is broken! For the past couple of hours, I've been getting this "Parameter name: '-2147483550' is not a valid value for 'index'." error. Apparently, its caused by having...
30
by: lgbjr | last post by:
hi All, I've decided to use Options Strict ON in one of my apps and now I'm trying to fix a late binding issue. I have 5 integer arrays: dim IA1(500), IA2(500), IA3(500), IA4(500), IA5(500) as...
10
by: Scott | last post by:
Ok, I've lost half a day to pounding my head against this one and I'm sending out a distress call... This is so simple that it's emberassing. However, VC# 2005 Express has made things so...
15
by: Tom Gur | last post by:
Hi, What is early/late binding ?
11
by: Lloyd Sheen | last post by:
Why oh why can I not edit and continue??? VS 2005 Pro. Multi project solution. All solutions set to debug. But I cannot edit the source when debugging. Any ideas??
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.