[Python-ideas] Optional parameters without default value
Steven D'Aprano
steve at pearwood.info
Thu Mar 2 09:24:14 EST 2017
On Thu, Mar 02, 2017 at 10:03:29AM +0200, Serhiy Storchaka wrote:
> I propose to add a new syntax for optional parameters. If the argument
> corresponding to the optional parameter without default value is not
> specified, the parameter takes no value. As well as the "*" prefix means
> "arbitrary number of positional parameters", the prefix "?" can mean
> "single optional parameter".
I like this! If the caller doesn't provide a value, the parameter
remains unbound and any attempt to look it up will give a NameError or
UnboundLocalError.
The only question is, how often do we need a function with optional
parameter that don't have a default? I've done it a few times, and used
the sentinel trick, but I'm not sure this is common enough to need
support from the compiler.
It is a really clever solution though.
> Alternative syntaxes:
>
> * "=" not followed by an expression: "def get(store, key, default=)".
Too easy to happen by accident if you accidently forget to add the
default, or delete it.
> * The "del" keyword: "def get(store, key, del default)".
This feature has nothing to do with del.
--
Steve
More information about the Python-ideas
mailing list