
On Wed, Oct 27, 2021 at 11:41 AM Christopher Barker <pythonchb@gmail.com> wrote:
I've been thinking about this from the perspective of a teacher or Python. I"m not looking forward to having one more thing to teach about function definitions -- I struggle enough with cover all of the *args, **kwargs, keyword-only, positional-only options.
Python used to be such a simple language, not so much anymore :-(
Yeah. My intention here is that this should be completely orthogonal to argument types (positional-only, positional-or-keyword, keyword-only), completely orthogonal to type hints (or, as I'm discovering as I read through the grammar, type comments), and as much as possible else. The new way will look very similar to the existing way of writing defaults, because it's still defining the default.
That being said, we currently have to teach, fairly early on, the consequences of using a mutable as a default value. And this PEP would make that easier to cover. But I think it's really important to keep the semantics as simple as possible, and left-to-right name binding is they way to do that.
Yes. It will now be possible to say "to construct a new list every time, write it like this" instead of drastically changing the style of code. def add_item(thing, target=[]): print("Oops, that uses a single shared default target") def add_item(thing, target=>[]): print("If you don't specify a target, it makes a new list")
(all this complicated by the fact that there is a LOT of code and advice in the wild about the None idiom, but what can you do?)
And that's not all going away. There will always be some situations where you can't define the default with an expression. The idea that a parameter is optional, but doesn't have a value, may itself be worth exploring (maybe some way to say "arg=pass" and then have an operator "if unset arg:" that returns True if it's unbound), but that's for another day. ChrisA