Suggesting a new feature - "Inverse Generators"

Jordan Rastrick jrastrick at student.usyd.edu.au
Sat Mar 26 01:56:28 EST 2005


> No, it's nothing special about groupby.  record simply stores its
state in a
> mutable default parameter.  This isn't general good practice: at
least you have
> to be careful with it.  You can see the behavior in the following
example:
>   >>> def accumulate(value, accum = []):
>   ...     accum.append(value)
>   ...     return accum
>   ...
>   >>> accumulate(1)
>   [1]
>   >>> accumulate(2)
>   [1, 2]
>   >>> accumulate(6)
>   [1, 2, 6]
>   >>>

Wow.... I'd never seen this kind of thing in examples of Python code.
Although its really neat, it doesn't really make sense, intuituvely to
me. Why does accum remember its state - I suppose its to do with the
scope of arguments (as opposed to method variables) or something like
that?

Still, thats powerful. But I see why its not standard use -  it could
be easily abused!




More information about the Python-list mailing list