Relax Syntax for Augmented Arithmetic?
Steven D'Aprano
steven at REMOVE.THIS.cybersource.com.au
Sun Jan 18 22:36:01 EST 2009
On Sun, 18 Jan 2009 15:11:46 -0500, Terry Reedy wrote:
> andrew cooke wrote:
>> Context -
>> http://docs.python.org/3.0/reference/datamodel.html?highlight=data
>> model#object.__iadd__
>>
>> Just a suggestion I thought I'd throw out... There's a restriction in
>> the language implementation on exactly what can go the left of an
>> augmented arithmetic expression.
>>
>> For example:
>>>>> a = 3
>>>>> a **= 2
>>
>> is ok, but:
>>>>> class Foo():
>> ... def __init__():
>> ... self.a = 3
>> ... def __ipow__(self, x):
>> ... self.a **= x
>> ...
>>>>> Foo() **= 2
>
> Calls return objects and therefore cannot be the target of an
> assignment, augmented or otherwise. The target of an assignment is a
> name or collection slot, both of which are grammatical constructs, not
> objects.
There's a practical reason too. You create a new Foo instance, mutate it
with the augmented assignment operator, and then a tenth of a millisecond
later the garbage collector throws it away because it has a reference
count of zero.
--
Steven
More information about the Python-list
mailing list