Binding frustration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Hunter

    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


  • JCM

    #2
    Re: Binding frustration

    Rob Hunter <rob@cs.brown.e du> wrote:
    ....
    [color=blue]
    > def getGenres(title ): #it takes a movie title and returns a list of
    > genres that the movie falls into[/color]
    [color=blue]
    > result = [] # my accumulator[/color]
    [color=blue]
    > 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[/color]

    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.

    Comment

    • Terry Reedy

      #3
      Re: Binding frustration


      "Rob Hunter" <rob@cs.brown.e du> wrote in message
      news:mailman.10 63920805.24865. python-list@python.org ...[color=blue]
      > def inGenre(g): # g is a genre
      > if <here I test if "title" is of genre g (using a simple[/color]
      Python[color=blue]
      > dictionary I have collected from a mini web crawl)>:
      > result = result + [g] # if title is of genre g, then[/color]
      add[color=blue]
      > it to the accumulator
      >
      > UnboundLocalErr or: local variable 'result' referenced before[/color]
      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


      Comment

      Working...