Assignment operator?
Ante Bagaric
abagaric_NOSPAM at rest-art.hr
Thu Apr 18 10:20:07 EDT 2002
After reading that PEP about static namespaces I'm now pretty confused with
the behaviour of the += operator.
The PEP states that if there is a binding of certain name anywhere within
the body of the function, it is considered local. Then it states that
binding operations include stuff like def, for, except... AND assignment.
I guess that for some reason operator += is considered an 'assignment'
(because it is often called 'one of assignment operators') but is it really?
Is 'a = a + 2' the _same_ as 'a += 2'? I don't think it is, or should be.
The 'a = a + 2' means 'store the value of a + 2 to the variable a' and 'a +=
2'
means 'increment a by 2'.
The fact is that Python already behaves exactly like that. If you say
>>> li = [1,2,3]
>>> li2 = li
>>> li += [4,5]
>>> li is li2
1
but if you say
>>> li = [1,2,3]
>>> li2 = li
>>> li = li + [4,5]
>>> li is li2
0
According to this, += operator can't be though of as an 'assignment
operator'.
And conversely, it shouldn't cause the compiler to consider 'foo' to be
local
if there is 'foo += stuff' somewhere within the body of the function.
After all, 'foo.append(stuff)' or 'foo.extend(stuff)' don't do it.
Or there is something I didn't quite understand?
Ante.
More information about the Python-list
mailing list