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
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
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
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
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
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
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.
>>
>>
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
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
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
__, 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
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
> 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
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
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 = []):
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
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
17 matches
Mail list logo