
On Sun, Oct 24, 2021 at 1:12 PM Ricky Teachey <ricky@teachey.org> wrote:
This might be a dumb question but is the context built only left to right? Or will this be legal?
def f(a, *, b=:c, c=:b): print(f"{a=} {b=} {c=}")
f(1, b=2) a=1 b=2 c=2
f(1, c=2) a=1 b=2 c=2
That's something where we may need to get a reference implementation before deciding, but I am open to either of two possibilities: 1) Keyword args are resolved before late-bound defaults, so your example would technically work, despite being confusing 2) Late-bound defaults explicitly reject (with SyntaxError) any references to arguments to their right. Python already enforces this kind of check:
def f(): ... x = 1 ... global x ... File "<stdin>", line 3 SyntaxError: name 'x' is assigned to before global declaration
Even if it IS legal, I would say that this would be something to avoid. Same with using assignment expressions to mutate other arguments - I suspect that this won't cause technical problems, but it would certainly hurt the brains of people who read it: def f(a, b=>c:=a, c=>b:=len(a)): print("wat") Yeah, just don't :) Not a dumb question. It's a little underspecified in the PEP at the moment (I only mention that they may refer to previous values, but not whether subsequently-named arguments are fair game), and am open to discussion about whether this should be locked down. Currently, I'm inclined to be permissive, and let people put anything they like there, just like you can put crazy expressions into other places where they probably wouldn't improve your code :) ChrisA