incrementation operators?

Thomas Wouters thomas at xs4all.net
Mon Aug 14 08:06:33 EDT 2000


On Sun, Aug 13, 2000 at 08:42:08PM -0400, Colin J. Williams wrote:

> I think you make a case for the list augmentation, but I still wonder
> about the frequency with which the simple arithmetic incrementing
> operators are used in other languages.  However, Guido has spoken and so I
> will not pursue that.

Your problem is that you see them as arithmatic operators. They needn't be, 
as proves the use of the 'list concat' and 'list repeat' operators :-)
Python classes and extention types can bind any kind of meaning they want to
those operators.

> Regarding the use of the += with lists.  It seems that:

> a= [1, 2]
> a+= 3                  is equivalent to a.append(3)
> a+= [4]                is equivalent to a.extend([4])

> Suppose one wishes to do a.append([5]).  It would appear that one would have to
> return to the existing syntax.

Augmented assignment does not touch the semantics of any of the binary
operators, other than providing a different set of 'hooks'. Your example
wouldn't be valid without augmented assignment:

>>> a = [1, 2]
>>> a = a + 3
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: can only concatenate list (not "int") to list

So it isn't valid with augmented assignment either:

>>> a += 3
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: argument to += must be a sequence

"a += 3" isn't equivalent to a.append(3), it's equivalent to
'a.extend(3)'. But you can write 'a.append(3)' as 'a.append([3])', and it's
solved :-) You might have missed that in my original posting: I did say you
could use augmented assignment in place of 'append' and 'extend', but my
example had that extra pair of brackets you need to make it work.

-- 
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