[Python-bugs-list] [ python-Feature Requests-753600 ] why should += produce name binding?

SourceForge.net noreply@sourceforge.net
Mon, 16 Jun 2003 07:20:44 -0700


Feature Requests item #753600, was opened at 2003-06-12 17:16
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=753600&group_id=5470

Category: Parser/Compiler
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Gregory Smith (gregsmith)
Assigned to: Nobody/Anonymous (nobody)
Summary: why should += produce name binding?

Initial Comment:

Currently, in

def x():
     foo += 1

... 'foo' is a local variable. Why should it be?
If the semantics are changed so that augmented
assignment is not a name-binding operation,
then only broken code will be affected.

This would allow you to use simple things
like   'EventCount += 1' without having to
use 'global EventCount'. After all, I can do
'EventList.append(...)' without the global decl.

For another (better) example, see 
http://mail.python.org/pipermail/edu-sig/2001-June/001329.html

In the response to the above, the poster is referred
to PEP227 which lists 'assignment' as a name-binding
operation. There is no clear-cut implication that this
includes augmented assignment, and in the Python
ref manual, one can only infer this behaviour from
the statement that x += y is almost equivalent
to x = x+y, which is pretty weak. In any case, since
an augmented assignment to a name always requires the
a-priori existence of that name in
an accessible namespace,  IMHO it should not
produce a binding.


















----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2003-06-16 09:20

Message:
Logged In: YES 
user_id=80475

In your example, I prefer the global declaration for 
EventCount.  It adds clarity and allows the reader to 
understand the function without reading the enclosing 
scopes.

I do agree the the += semantics are thorny area for python, 
I prefer the current binding behavior which is what I expect 
out of an assignment operation.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2003-06-14 11:24

Message:
Logged In: YES 
user_id=21627

Can you please explain, in detail, what semantics you
propose for += if the variable does not refer to a mutable
object?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=753600&group_id=5470