[Python-Dev] Re: Is it just Syntactic Sugar ?

Thomas Wouters thomas@xs4all.net
Sun, 4 Jun 2000 00:56:01 +0200


On Wed, May 31, 2000 at 11:49:57PM +0000, Guido van Rossum wrote:

> I know I shouldn't be posting in this thread, and I won't be there to read
> the responses, but here's what I thought would be cool.

As the 'lucky, lucky bastard' is probably standing before the altar about
now, I'll send this to the dev-list instead. Not that I expect anyone *not*
to be at the wedding, but I bet the rest is back sooner ;-)

> x+=y is syntactic sugar for x=x.__add_ab__(y); the "ab" means "and becomes"
> (an old Algol-68 naming convention; we could pick something better later
> but this will do for the explanation).

> For immutable types, this is defined as

>   def __add_ab__(self, other):
>       return self+other

> For mutable types, this is defined as a self-mutating operation,
> e.g. for lists it could be

>   def __add_ab__(self, other):
>       self.extend(other)
>       return self

This was what I had in mind, and was trying to explain. Does you voicing
your opinion mean someone (you ? someone else ?) is working on this, or soon
going to work on this ? I had a chat with Michael about fixing up his patch
to work all the way (it's currently a proof-of-concept-quick-hack that only
works for builtin types) which prompted me to study Python's internals a bit
closer. (I hope I dont sound too patronizing when I say I was impressed ;-)

I'm curious what should happen with index-assignment and slice-assignment:

x[y] += z
x[:y] += z

(Obviously this wont make sense for a lot of types, or will be too
un-obvious to include, but I can imagine matrix-types to happily add this.)

Would this call x.__add_item_ab__(y, z) and x.__add_slice_ab__(0, y, z) ? Or
would x[y] += z always call x[y].__add_item_ab__() and x[:y] create a new
object, a slice of x, and call its __add_ab__() method ? Or would it try all
of them, or more, until it gets a good result ? Or am I running ahead of
things and should we wait for a working patch first ? :)

If I suddenly grow a deep understanding of Python's internals (not ruled
out, it's fairly obvious) and hack-up something that works before anyone
else, I'll be sure to mail ;)

oh-and-congratulations-to-Mrs.-van-Rossum-too-ly yr's,
-- 
Thomas Wouters <thomas@xs4all.net>

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