Binding frustration

Terry Reedy tjreedy at udel.edu
Fri Sep 19 00:21:38 CEST 2003


"Rob Hunter" <rob at cs.brown.edu> wrote in message
news:mailman.1063920805.24865.python-list at python.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
>
> 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






More information about the Python-list mailing list