On Thu, Feb 11, 2021 at 6:03 PM Ross Wilson <rzzzwil...@gmail.com> wrote: > > On Thu, 11 Feb 2564 BE at 12:52 Grant Edwards <grant.b.edwa...@gmail.com> > wrote: > > > On 2021-02-11, J. Pic <j...@yourlabs.org> wrote: > > > > > I just meant removing the whole "default value mutating" story, not > > > removing mutable variables. Really, I was wondering if there was a use > > case > > > where this actually turns to an advantage, > > > > I've seen people show how it can be used to provide function-scope > > persistent storage -- the equivalent of declaring a static variable in > > a C function. I don't think I've seen that done in the wild, though. > > > Not sure this qualifies as "use in the wild", but the memoized naive > Fibonacci is very nice when written this way: > > def fib(n, memo={0: 0, 1: 1}): > if n not in memo: > memo[n] = fib(n-1) + fib(n-2) > return memo[n]
Yep, that's a pretty elegant example of mutable defaults as statics. In theory, you could use this for a Fibonacci-like sequence, just by passing an appropriate memo dictionary; to make that work, the recursive calls would have to pass that down the line. # What comes next in the sequence 1 3 4 7 11? fib(6, {1:1, 2:3}) But otherwise, the memo parameter isn't really being used as a parameter, it's just a way to maintain state. It could just as easily be a global, a closure cell, or anything else. ChrisA -- https://mail.python.org/mailman/listinfo/python-list