Re: Mutable defaults

2021-02-11 Thread J. Pic
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 coul

Re: Mutable defaults

2021-02-10 Thread Chris Angelico
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 i

Re: Mutable defaults

2021-02-10 Thread Ross Wilson
On Thu, 11 Feb 2564 BE at 12:52 Grant Edwards wrote: > On 2021-02-11, J. Pic 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

Re: Mutable defaults

2021-02-10 Thread Grant Edwards
On 2021-02-11, J. Pic 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 persist

Re: Mutable defaults

2021-02-10 Thread J. Pic
Ok, maybe: def foo(x=:[], y=:len(x)): As a shorthand for: @default(x=lambda: [], y=lambda x: len(x)) def foo(x=None, y=None): =: Is the contraction for =lambda: At the same time, this new syntax avoid breaking def foo(x=lamba: []) which is completely different as we know. Le jeu. 11 févr. 202

Re: Mutable defaults

2021-02-10 Thread Chris Angelico
On Thu, Feb 11, 2021 at 1:55 PM J. Pic wrote: > > Adding decorators with some inspect sauce could certainly work with the > syntax we already have: > > @default(x=lambda: copy([]), y=lambda x: len(x)) > def foo(x=None, y=None): This would work, although the copy is unnecessary here. But you're a

Re: Mutable defaults

2021-02-10 Thread J. Pic
weren't asking about removing mutables altogether. >> But how would you remove "default value mutating"? Do you disallow any >> mutable values from being argument defaults? Because that's a huge >> category of values that are no longer available. >> >>

Re: Mutable defaults

2021-02-10 Thread J. Pic
any > mutable values from being argument defaults? Because that's a huge > category of values that are no longer available. > > Mutable defaults most certainly ARE used deliberately, although often > it's more as a sort of "static variable" rather than actually

Re: Mutable defaults

2021-02-10 Thread Chris Angelico
hat's a huge category of values that are no longer available. Mutable defaults most certainly ARE used deliberately, although often it's more as a sort of "static variable" rather than actually a parameter. But it's common enough as an idiom that it can't be changed wi

Re: Mutable defaults

2021-02-10 Thread J. Pic
default? > > > > > or doing copy on call by default. > > > > I assume you are referring only to argument defaults, because > > copy-on-call for ALL parameters would be ridiculously costly, not to > > mention problematic in many ways. > > > > T

Re: Mutable defaults

2021-02-10 Thread Paul Bryan
__, as a default? > > > or doing copy on call by default. > > I assume you are referring only to argument defaults, because > copy-on-call for ALL parameters would be ridiculously costly, not to > mention problematic in many ways. > > The main problem is that mutable de

Re: Mutable defaults

2021-02-10 Thread Chris Angelico
atic in many ways. The main problem is that mutable defaults are only very rarely an issue (for starters, you have to actually mutate the object - just because it's mutable, that doesn't mean you'll change anything), but any global "solution" to the "problem" is g

Re: Mutable defaults

2021-02-10 Thread J. Pic
> Most of us know of the perils of mutable default values. And those who don't pay the price. I wonder what would be the harm in removing them, or doing copy on call by default. -- https://mail.python.org/mailman/listinfo/python-list

Re: Mutable defaults

2021-02-09 Thread Terry Reedy
On 2/9/2021 10:17 AM, Antoon Pardon wrote: Most of us know of the perils of mutable default values. So I came up with the following proof of concept: Which is intended to do what? from inspect import signature as signature_of, Parameter from itertools import zip_longest from copy import copy

Re: Mutable defaults

2021-02-09 Thread Peter Otten
On 09/02/2021 16:17, Antoon Pardon wrote: Most of us know of the perils of mutable default values. So I came up with the following proof of concept: [...] def copy_defaults(f): Once you know you need that decorator you won't need it anymore ;) @copy_defaults def prepare(value, lst = []):

Re: Mutable defaults

2021-02-09 Thread Chris Angelico
On Wed, Feb 10, 2021 at 2:19 AM Antoon Pardon wrote: > > Most of us know of the perils of mutable default values. So I came up with > the following proof of concept: > > def copy_defaults(f): > > def wrapper(*args): > return f(*newargs) > return wrapper > > @copy_defaults > def

Mutable defaults

2021-02-09 Thread Antoon Pardon
Most of us know of the perils of mutable default values. So I came up with the following proof of concept: from inspect import signature as signature_of, Parameter from itertools import zip_longest from copy import copy def copy_defaults(f): signature = signature_of(f) def wrapper(*ar