Hello,

On Tue, 26 May 2020 08:37:59 +1000
Chris Angelico <ros...@gmail.com> wrote:

[]

> >         def foo(x := a + b)
> >
> > vs
> >
> >         c = a + b
> >         def foo(x := c)
> >
> > can lead to different results.  
> 
> def foo(x = None):
>     if x is None: x = a + b
> 
> c = a + b
> def foo(x = None):
>     if x is None: x = c
> 
> Is it surprising that these behave differently? You are refactoring
> something from a late-bound default argument value into a global
> variable. Surely it's obvious that it will now be evaluated once?

Of course it will be obvious - you provided explicit control flow to
make it work like that, and all that stays within the bounds of
the existing semantics of the language.

> > I'd suggest that people should love "explicit is better than
> > implicit" principle of the language.  
> 
> Explicit meaning that you need to use a specific symbol that means
> "this is to be late-bound"?

No, it means "use explicit 'if' if you want to deal with mutable
default".

> Or explicit meaning "something that I like", as opposed to implicit
> meaning "something that I don't like", which is how most people seem
> to interpret that line of the Zen?

I'd prefer to think in terms of implementation complexity. Implemented
in adhoc way (and that's how things get implemented in C, in
particular, in CPython), it will be quite a noticeable complexity
up-glitch to function representation/implementation, and all it
achieves is trading one confusion for another.

(Well, for two others: why the heck there're 2 ways to define default
args, which is to use when, and why one of them doesn't work across
subexpression refactoring. Oh, and old confusion still stays with us.
There's really no easy way to resolve the original confusion, short of
banging mutable defaults. Which actually one that I'd like, because it
would necessitate pushing "const" (currently, "Final") variable
annotation down to the core of the language, for Python remains
literally the last one which lacks it comparing to the competition.)

> 
> ChrisA


-- 
Best regards,
 Paul                          mailto:pmis...@gmail.com
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/BQAQ5YPU7T63BKRBDPSY3IDY5IVCM7WA/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to