On Sun, Oct 31, 2021 at 03:10:56PM +1100, Chris Angelico wrote:

> (Side point: The current reference implementation allows assignment
> expressions inside default argument expressions, mainly because I
> didn't go to the effort of blocking them. But if you ever ACTUALLY do
> this, then..... *wat*)

Why? The result is well-defined. It might not be *good* code, but let's 
not be overzealous with banning legal code just because it is bad :-)

That's the job of linters and code reviews.

In an early bound default, the walrus operator binds to a name in the 
scope the expression is evaluated in, i.e. the surrounding scope. So a 
top level function with a walrus:

    string = "hello"
    def func(spam=(a:=len(string)), eggs=2**a):
        return (spam, eggs)

binds to a global variable `a`.

Similarly, a walrus inside a late bound default should bind to a local 
variable. The walrus doesn't get executed until the function namespace 
is up and running, and the walrus is a binding operation, so it should 
count as a local variable.



-- 
Steve
_______________________________________________
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/2TRG5EDB5LTEOYH4FOICLBRU76K7OUPW/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to