accumulators
Michele Simionato
michele.simionato at poste.it
Sun Jun 13 02:33:10 EDT 2004
Leif K-Brooks <eurleif at ecritters.biz> wrote in message news:<j3Nyc.3335$Hf.1868685 at newshog.newsread.com>...
> Just for fun, a full-blown class with documentation and the like:
>
> class Accumulator(object):
> """This class implements a simple accumulator. Instate it with a
> starting value, or it will default to 0. It can be called with
> another value, which will be accumulated. The current value will
> also be returned.
>
> Example:
>
> >>> a = Accumulator(1)
> >>> a(2)
> 3
> >>> a(1)
> 4
> >>> a(3)
> 7
> """
>
> __slots__ = '_value'
>
> def __init__(self, value=0):
> self._value = value
>
> def __call__(self, value):
> self._value += value
> return self._value
>
> def __str__(self):
> return str(self._value)
>
> def __repr__(self):
> return "<Accumulator object with value %s>" % self._value
I just don't see the need to use __slots__ here. The first rule about __slots__
is: don't use them! OTOH the second rule (for expert only) is: don't use them!!
That's true for any optimization, isnt'it? ;)
Michele Simionato
More information about the Python-list
mailing list