Something in the function tutorial confused me.
Neil Cerutti
horpner at yahoo.com
Sat Aug 11 18:18:12 EDT 2007
> On 2007-08-11, Alex Martelli <aleax at mac.com> wrote:
>> Neil Cerutti <horpner at yahoo.com> wrote:
>> ...
>>> The Python Language Reference seems a little confused about
>>> the terminology.
>>>
>>> 3.4.7 Emulating numeric types
>>> 6.3.1 Augmented assignment statements
>>>
>>> The former refers to "augmented arithmetic operations", which
>>> I think is a nice terminology, since assignment is not
>>> necessarily taking place. Then the latter muddies the waters.
>>
>> Assignment *IS* "necessarily taking place"; if you try the
>> augmented assignment on something that DOESN'T support
>> assignment, you'll get an exception. Consider:
>>
>>>>> tup=([],)
>>>>> tup[0] += ['zap']
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> TypeError: 'tuple' object does not support item assignment
>>
>> Tuples don't support item ASSIGNMENT, and += is an ASSIGNMENT,
>> so tuples don't allow a += on any of their items.
>>
>> If you thought that += wasn't an assignment, this behavior and
>> error message would be very problematic; since the language
>> reference ISN'T confused and has things quite right, this
>> behavior and error message are perfectly consistent and clear.
>
> Thanks for the correction. I was under the illusion that
> sometimes augmented assignment would instead mutate the object.
OK, I've thought about this some more and I think the source of
my confusion was I thought assignment in Python meant binding a
name to something, not mutating an object. But in the case of
augmented assignment, assignment no longer means that?
--
Neil Cerutti
More information about the Python-list
mailing list