
On Sun, 24 Oct 2021, Chris Angelico wrote:
Is anyone interested in coauthoring this with me? Anyone who has strong interest in seeing this happen - whether you've been around the Python lists for years, or you're new and interested in getting involved for the first time, or anywhere in between!
I have a strong interest in seeing this happen, and would be happy to help how I can. Teaching (and using) the behavior of Python argument initializers is definitely a thorn in my side. :-) I'd love to be able to easily initalize an empty list/set/dict. For what it's worth, here are my thoughts on some of the syntaxes proposed so far: * I don't like `def f(arg => default)` exactly because it looks like a lambda, and so I imagine arg is an argument to that lambda, but the intended meaning has nothing to do with that. I understand lambdas give delegation, but in my mind that should look more like `def f(arg = => default)` or `def f(arg = () => default)` -- except these will have a different meaning (arg's default is a function, and they would be evaluated in parent scope not the function's scope) once `=>` is short-hand for lambda. * I find `def f(arg := default)` reasonable. I was actually thinking about this very issue before the thread started, and this was the syntax that came to mind. The main plus for this is that it uses an existing operator (so fewer to learn) and it is "another kind of assignment". The main minus is that it doesn't really have much to do with the walrus operator; we're not using the assigned value inline like `arg := default` would mean outside `def`. Then again, `def f(arg = default)` is quite different from `arg = default` outside `def`. * I find `def f(arg ?= default)` (or `def f(arg ??= default)`) reasonable, exactly because it is similar to None-aware operators (PEP 0505), which is currently/recently under discussion in python-dev). The main complaint about PEP 0505 in those discussions is that it's very None-specific, which feels biased. But the meaning of "omitted value" is extremely clear in a def. If both this were added and PEP 0505 were accepted, `def f(arg ?= default)` would be roughly equivalent to: ``` def f(arg = None): arg ??= default ``` except `def f(arg ?= default)` wouldn't trigger default because in the case of `f(None)`, whereas the above code would. I find this an acceptable difference. (FWIW, I'm also in favor of 0505.) * I also find `def f(@arg = default)` reasonable, though it feels a little inconsistent with decorators. I expect a decorator expression after @, not an argument, more like `def f(@later arg = default)`. * I'm not very familiar with thunks, but they seem a bit too magical for my liking. Evaluating argument defaults only sometimes (when they get read in the body) feels a bit unpredictable. Erik -- Erik Demaine | edemaine@mit.edu | http://erikdemaine.org/