Python dot-equals (syntax proposal)

Alf P. Steinbach alfps at start.no
Mon May 3 00:37:49 EDT 2010


* Terry Reedy:
> * Alf P. Steinbach:
>> * Aahz:
>
>>> and sometimes
>>> they rebind the original target to the same object.
>>
>> At the Python level that seems to be an undetectable null-operation.
>
> If you try t=(1,2,3); t[1]+=3, if very much matters that a rebind occurs.

Testing:

<test lang="py3">
 >>> t = ([], [], [])
 >>> t
([], [], [])
 >>> t[0] += ["blah"]
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
 >>> t
(['blah'], [], [])
 >>> _
</test>

Yep, it matters.

Is this change-but-raise-exception a bug?

I seem to have a knack for running into bugs. :-)


>> Granted one could see something going on in a machine code or byte code
>> debugger. But making that distinction (doing nothing versus
>> self-assignment) at the Python level seems, to me, to be meaningless.
>
> Please do not confuse things. Augmented *assignment* must be understood
> as assignment. Failure to do so leads (and has lead) newbies into
> confusion, and puzzled posts on this list.

OK.

But I think it would be less confusing, less breaking of expectations, if, for 
the example above, += reduced to the functionality of extend(), with no x.


Cheers, & thanks,

- Alf



More information about the Python-list mailing list