Compound Assignment Operators ( +=, *=, etc...)

M.-A. Lemburg mal at lemburg.com
Mon Aug 16 09:15:32 EDT 1999


Fredrik Lundh wrote:
> 
> Andrew Clover <esuzm at primrose.csv.warwick.ac.uk> wrote:
> > I'd like a syntax like:
> >
> >   x.+(1)
> >
> > With the idea that x is an object (of type int) with a '+' method that
> > adds stuff to it. It'd be nicer without the brackets though.
> 
> mutable integers?  now that's a really cool feature! (known
> from Fortran and Tcl, among others. I'm pretty sure Perl
> has them too ;-).

Here's a mutable integer implementation: it's a counter
type with which you can do cute things like:

>>> c = Counter(0)
>>> ++c
1
>>> ++c
2

You can even split the increment into two operations (well, that's
due to the hack used to implement this):

>>> +c
2
>>> +c
3

Here's some more magic:

>>> +-c
3
>>> -+c
4
>>> -+c
4
>>> -+c
4
>>> +-c
3
>>> +-c
3

To find out how this works, see:

	http://starship.skyport.net/~lemburg/mxCounter-0.1.zip

-- 
Marc-Andre Lemburg
______________________________________________________________________
Y2000:                                                   138 days left
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/





More information about the Python-list mailing list