[Python-ideas] from __future__ import runtime_default_kwargs

Chris Angelico rosuav at gmail.com
Mon Mar 11 07:33:26 EDT 2019


On Mon, Mar 11, 2019 at 10:28 PM James Lu <jamtlu at gmail.com> wrote:
>
> When
>
> from __future__ import runtime_default_kwargs
>
>
>
> Is run,
>
> def a(b=1, c=b+2, d=[]):
>     pass
>
> behaves as (if the peephole optimizer didn’t exist)
>
> def a(b=None, c=None):
>     if b is None:
>         b = 1
>     if c is None:
>         c = b + 2
>     if d is None:
>         d = []
>
> i.e. the keyword expression is evaluated at runtime.
>
> Perhaps a restriction on “literals only” can be made so people don’t abuse this.

A future directive is appropriate only if the language is planned to
be changed. Is that what you're actually asking for? If so, ask for
that (and then the future directive becomes a transition tool).

As an alternative, consider tagging the specific function, not the
whole module (future directives always apply to the entire module).
The best way to tag a function would probably be a decorator.

ChrisA


More information about the Python-ideas mailing list