On 02/12/2021 03:35, Steven D'Aprano wrote:
On Wed, Dec 01, 2021 at 05:16:34PM +1100, Chris Angelico wrote:
1) If this feature existed in Python 3.11 exactly as described, would you use it? Yes I would, but probably not as often as counting cases of the "if param is None: ..." idiom might lead you to expect.
The problem is, as Neil also pointed out, that it becomes tricky to explicitly ask for the default behaviour except by leaving the argument out altogether. Not impossible, as Chris mentions elsewhere, you can mess about with `*args` or `**kwargs`, but it is decidedly less convenient, more verbose, and likely to have a performance hit.
So if I were messing about in the interactive interpreter, I would totally use this for the convenience:
def func(L=>[]): ...
but if I were writing a library, I reckon that probably at least half the time I'll stick to the old idiom so I can document the parameter:
"If param is missing **or None**, the default if blah..."
I reject Chris' characterisation of this as a hack. There are function parameters where None will *never* in any conceivable circumstances become a valid argument value, and it is safe to use it as a sentinel.
There are also circumstances when you start off thinking that None will never be a valid argument value, but later you have to cater for it. Now it does start to look as if you used a hack. Best wishes Rob Cliffe