News for hackers. Stuff that matters.
Re:Strict Python function parameters by meowface on Sunday January 23, 2022 @11:45AM (#30047629)
Sounds like a good idea. Not really a fan of the suggested syntax, though:

  def fn(s, n=>len(s)):
"=>" just kind of bugs me, for some reason. (I love me some bikeshedding.)

Of the alternatives they offer, ":=" kind of looks nicest to me:

  def fn(s, n:=len(s)):
Though it doesn't really line up with the semantics of the existing walrus operator. Also, as the PEP mentions, it could look confusing when mixed with type annotations.

Semantically, the "?=" alternative might be best, since it kind of makes it clear it's a default, and doesn't conflict with anything else in the language:

  def fn(s, n?=len(s)):
Kind of reads like "present? else = ...".

edit: At this point I'd shift from "might be best" to "almost certainly is best". I'm now a zealous "?=" advocate.

Re:Strict Python function parameters by kortex on Sunday January 23, 2022 @12:02PM (#30047800)
I agree. I think python needs some solution to the `def foo(mylist=[])` which is a mistake I see devs make all the time. `foo=>func(a)` is awful. There's too much precedent that `x = f => {}` defines a lambda with x as a parameter. This flips it on its head. `@` would be challenging for the parser. `:=` also does not match the walrus semantics.

`x ?= default` is perfect IMHO. There's already prior art (Makefiles) that this means exactly "present? else = ..." with lazy evaluation of RHS expression.

It also would allow eliminating the "if x is SENTINEL" pattern, where None is a valid non-default argument.

Re:Strict Python function parameters by klyrs on Sunday January 23, 2022 @03:35PM (#30050178)
Having mylist stick around is actually useful for making stateful functions. For example, I've often used

  def foo(..., cache={}):
    ...
to implement cached functions, or functions which need an internal cache for whatever reason. Later versions of Python provide an lru cache decorator, but that doesn't help with the internal cache and can be inefficient when you don't want your cache to forget any results.
Re:Strict Python function parameters by meowface on Sunday January 23, 2022 @12:08PM (#30047862)
Yeah, the more I look at it, the more I'm a fan of "?=". Kind of seems like the obvious choice at this point.

The only potential downside is that standard, function definition-time default arguments also imply "present? else = ...". But I think of all the options available, this one makes the most sense.

Re:Strict Python function parameters by throwaway775888 on Sunday January 23, 2022 @04:27PM (#30050595)
What about .&= for default arguments?, inspired by Ruby.
Re:Strict Python function parameters by softwaredoug on Sunday January 23, 2022 @12:15PM (#30047958)
I don't think its bike shedding FWIW, Python traditionally eschews fewer keywords and more obvious operators. So it's a reasonable concern.
Re:Strict Python function parameters by meowface on Sunday January 23, 2022 @12:18PM (#30047993)
Yeah, it's probably not bikeshedding when it concerns a very popular programming language's syntax. I just find myself prone to bikeshedding and pedantic naming indecisiveness in general.