Cameron Laird wrote:
> In article <[EMAIL PROTECTED]>,
> Paddy <[EMAIL PROTECTED]> wrote:
>                       .
>               [substantial thread
>               with many serious
>               alternatives]
>                       .
>                       .
> >You can do things with function attributes
> >
> >def foo(x):
> >  foo.static += x
> >  return foo.static
> >foo.static = 0
>                       .
>                       .
>                       .
> My favorite variation is this:
>
>   def accumulator(x):
>           # On first execution, the attribute is not yet known.
>       # This technique allows use of accumulator() as a
>       # function without the "consumer" having to initialize
>       # it.
>       if not "static" in dir(accumulator):
>       accumulator.static = 0
>       accumulator.static += x
>       return accumulator.static
>
>   print accumulator(3)
>   print accumulator(5)

Thanks Cameron, I'll accumulate this in my toolbox.

- pad.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to