Augmented Assignment (was: Re: PEP scepticism)

Guido van Rossum guido at python.org
Fri Jul 6 13:48:18 CEST 2001


Thomas Wouters <thomas at xs4all.net> writes:

> On Mon, Jul 02, 2001 at 02:00:51AM +0000, Rainer Deyke wrote:
> > An interesting (and AFAIK legal) optimization would be to make
> > tuples act as mutable objects with respect to augmented assignment
> > if their reference is 1.
> 
> But they are never 1 unless you're in a C extention that just created it.
> When you call 'x += y', the refcount for both x and y is always 2 when the
> arguments arrive at the __iadd__ (or C equivalent.)
> 
> So you mean '2'. But then you could run into trouble on, say,
> 
>   () += y
> 
> because the empty tuple is shared, just like interned strings and integers.
> The refcount *could* be 2, and you end up modifying someone else's empty
> tuple.

No, because "() += y" is syntactically illegal.

It wouldn't be a useful optimization anyway, because += on a tuple
would have to reallocate the tuple, which is tantamount to allocating
a new one anyway, thus losing most of the advantage.

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-list mailing list