Binding frustration

Discussion in 'Python' started by Rob Hunter, Sep 18, 2003.

  1. Rob Hunter

    Rob Hunter Guest

    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
     
    Rob Hunter, Sep 18, 2003
    #1
    1. Advertising

  2. Rob Hunter

    JCM Guest

    Rob Hunter <> 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.
     
    JCM, Sep 18, 2003
    #2
    1. Advertising

  3. Rob Hunter

    Terry Reedy Guest

    "Rob Hunter" <> wrote in message
    news:...
    > 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
     
    Terry Reedy, Sep 18, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MattG

    Crystal Frustration

    MattG, Nov 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    430
    MattG
    Nov 26, 2003
  2. Rob

    frustration with events firing

    Rob, Sep 10, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    342
    Hermit Dave
    Sep 11, 2004
  3. scorpion53061

    ASP.NET 2.0 Frustration - MS Please Help

    scorpion53061, Nov 25, 2005, in forum: ASP .Net
    Replies:
    9
    Views:
    3,508
    Scott Allen
    Nov 28, 2005
  4. msorvino
    Replies:
    1
    Views:
    7,061
    Wendy S
    Jan 13, 2005
  5. Rob Hunter

    Re: Binding frustration

    Rob Hunter, Sep 18, 2003, in forum: Python
    Replies:
    2
    Views:
    273
    Terry Reedy
    Sep 19, 2003
Loading...

Share This Page