Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Binding frustration

Reply
Thread Tools

Re: Binding frustration

 
 
Rob Hunter
Guest
Posts: n/a
 
      09-18-2003

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

>
> "Rob Hunter" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
>



 
Reply With Quote
 
 
 
 
Ulrich Petri
Guest
Posts: n/a
 
      09-19-2003

"Rob Hunter" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
>
> 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


 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      09-19-2003

"Ulrich Petri" <(E-Mail Removed)> wrote in message
news:bkesft$105nn$(E-Mail Removed)-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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GridView binding - how to stop initial binding Amit ASP .Net 6 10-24-2006 08:06 AM
pywin32 COM sort in Excel (late binding fails, early binding works) (+py2exe) kogrover@gmail.com Python 2 10-20-2006 04:08 PM
Complex data binding question, binding child objects of a custom collection. JcFx ASP .Net Datagrid Control 0 06-01-2005 04:01 PM
Data Binding - using inline code vs. functions vs. straight binding Jordan ASP .Net 2 02-10-2004 08:32 PM
Binding frustration Rob Hunter Python 2 09-18-2003 10:21 PM



Advertisments