
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