Binding frustration

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

"Rob Hunter" <rob at> wrote in message
news:mailman.1063920805.24865.python-list at
>      def inGenre(g): # g is a genre
>          if <here I test if "title" is of genre g (using a simple
> dictionary I have collected from a mini web crawl)>:
>                result = result + [g] # if title is of genre g, then
> it to the accumulator
> UnboundLocalError: local variable 'result' referenced before

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