Augmented assignment again

Thomas Wouters thomas at xs4all.net
Wed Aug 30 03:49:02 EDT 2000


On Wed, Aug 30, 2000 at 08:28:58AM +0200, Rpoeland Rengelink wrote:

> I've been working recently on wrapping an image manipulation library
> that only provides in-place arithmetic operators. I've tried
> to overload the normal arithmetic operators in terms of
> copies and augmented assignment operators with somewhat mixed results.

It's not possible to do *only* in-place operations, in Python, unless you
forgo all the normal arithmatic operators. And that means not writing

d = a+b+c

but

d = a # or a.copy()
d += b
d += c

As soon as you mix normal binary operators in there, you will get new
objects.

> What I would really like is something like:

> class Obj:
>     ...
>  
>     def __assign__(self):
>         self.is_temporary = 0 

> I.e., a function that's invoked whenever an instance is named

An instance isn't "named", it's bound to a name in the local namespace. But
that doesn't mean it's not "finalized" until then! And a lot of other
bindings are possible. Consider

l[1] = a+b+c

and

x.d = a+b+c

Those aren't 'named', but they should be considered 'finalized'. I think
you'll either have to do it manually, or stick to augmented assignment
operators (or just create the new objects when the programmer asks for it,
and educate them on the consequences. If they want to create copies, they
can do that anyway!)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list