473,805 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('Suspen se')
...
return result

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

UnboundLocalErr or: 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 1477
JCM
Rob Hunter <ro*@cs.brown.e du> 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.e du> wrote in message
news:ma******** *************** ***********@pyt hon.org...
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

UnboundLocalErr or: 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
11057
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 figure it out easily: ----- #! /python a = (recipient's e-mail address) pgp -feat a
0
2353
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 visual representation of data. Two types of data binding are available for Windows Forms: Simple Data Binding and Complex Data Binding. Simple data binding allows you to bind one data element to a control. In many situations you want to display...
9
10431
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 As DAO.Workspace Dim db As DAO.Database Dim qdf As DAO.QueryDef Dim rs As DAO.Recordset
1
2635
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 Products table.
0
4809
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 manually inserted a row in the table bound to the Combo box. The InsertAt Method of adding a row just does not work. Hope this helps anyone with this problem. john
30
2840
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 integer The integers in these arrays are actually pointers to different columns of data in a text file.
10
1679
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 monkey-proof that they made it hard to find out how to do the simple things that don't have drag&drop tools. This is the code in the sample that is being used to access an .mdb file: ******************** foreach (System.Data.DataRow theRow in...
15
3117
by: Tom Gur | last post by:
Hi, What is early/late binding ?
11
1335
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
9716
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
9596
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
10604
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
9179
agi2029
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...
1
7644
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
5536
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...
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
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...

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.