Scott David Daniels Scott.Daniels at Acm.Org
Sat Jun 26 00:15:25 CEST 2004

Paul Rubin wrote:

> ... The Pythonic way to do it is with a class instance:
>   class accum:
>     def __init__(self, n):
>        self.s = n
>     def __call__(self, i):
>        self.s += i
>        return self.s
>   a = accum(3)
>   (etc.)
> however, for programmers comfortable with the Lisp idioms of using
> internal lambdas, the class/object approach is cumbersome.

The way I'd do it is:

    class accum:
       def __init__(self, start):
          self.runningtotal = start

       def increment(self, value):
          self.runningtotal += value
          return self.runningtotal

    a = accum(3).increment
Then you can use:
    a(3) ...

That is, avoid magic names unless needed, and make the names obvious.

-Scott David Daniels
Scott.Daniels at Acm.Org

More information about the Python-list mailing list