Re: Binding frustration

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

  1. Rob Hunter

    Rob Hunter Guest

    On Thursday, September 18, 2003, at 06:21 PM, Terry Reedy wrote:

    >
    > "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).


    So it seems that if Python made a syntactic distinction between
    introducing a new binding, and assigning to an old one, then this
    problem would go away. That's what is happening with result.append(g),
    right? Because of the syntax in this statement (ie, there's no "=" in
    it), Python knows that _result_ must exist, so it goes and looks for
    it. But, IMO, I *should* be allowed to use the + (append) operator.
    Because there is no syntax for a new binding (like LET, for example),
    Python awkwardly forces the programmer to choose either the local scope
    or the global one when it comes to assignment. And so, in my case, it
    forces me to use another (but semantically, quite similar) language
    construct.

    Does anyone know why they didn't have an explicitly new binding syntax?
    Perhaps to minimize the initial part of the learning curve. This
    makes sense, but I would have much preferred it the other way.

    Rob

    >
    > Terry J. Reedy
    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Rob Hunter, Sep 18, 2003
    #1
    1. Advertising

  2. Rob Hunter

    Ulrich Petri Guest

    "Rob Hunter" <> schrieb im Newsbeitrag
    news:...
    >
    > So it seems that if Python made a syntactic distinction between
    > introducing a new binding, and assigning to an old one, then this
    > problem would go away. That's what is happening with result.append(g),
    > right? Because of the syntax in this statement (ie, there's no "=" in
    > it), Python knows that _result_ must exist, so it goes and looks for
    > it. But, IMO, I *should* be allowed to use the + (append) operator.
    > Because there is no syntax for a new binding (like LET, for example),
    > Python awkwardly forces the programmer to choose either the local scope
    > or the global one when it comes to assignment. And so, in my case, it
    > forces me to use another (but semantically, quite similar) language
    > construct.
    >


    What result.append(bla) does is not an "assignement to an old binding". It
    mutates the object that is bound to the name "result". In order to do so
    Python looks up the name in the local namespace if it isn't found there it
    looks in the namespace above and so on.

    Whenever you use the assignment syntax (foo = bar) you create a "new"
    binding unless you have declared the name global.

    HTH

    Ciao Ulrich
    Ulrich Petri, Sep 19, 2003
    #2
    1. Advertising

  3. Rob Hunter

    Terry Reedy Guest

    "Ulrich Petri" <> wrote in message
    news:bkesft$105nn$-berlin.de...
    > Whenever you use the assignment syntax (foo = bar) you create a

    "new"
    > binding unless you have declared the name global.


    Name assignment (name = expr) always creates a new binding (and
    deletes any previous binding of the same name). The 'global'
    declaration only affects which namespace the binding is made in. I
    believe that any sense in which foo=bar would not make a 'new' binding
    would be equally applicable to local as well as global binding.

    Terry J. Reedy
    Terry Reedy, Sep 19, 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:
    411
    MattG
    Nov 26, 2003
  2. Rob

    frustration with events firing

    Rob, Sep 10, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    323
    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,481
    Scott Allen
    Nov 28, 2005
  4. msorvino
    Replies:
    1
    Views:
    7,015
    Wendy S
    Jan 13, 2005
  5. Rob Hunter

    Binding frustration

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

Share This Page