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.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.
`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.