[Python-ideas] Optional parameters without default value
Brett Cannon
brett at python.org
Thu Mar 2 13:37:18 EST 2017
On Thu, 2 Mar 2017 at 08:58 Ethan Furman <ethan at stoneleaf.us> wrote:
> On 03/02/2017 08:13 AM, Joao S. O. Bueno wrote:
>
> > Is it just me that find that having the un-assigned parameter raise
> > NameError (or other exception) much more cumbersome than
> > havign a sentinel-value?
>
> No. While clever, the hassle of figuring out if you have a parameter
> clearly outweighs the benefit of avoiding a
> sentinel value.
>
> It would be a different story if we had exception-catching expressions. ;)
>
I don't like the NameError solution either.
What I would like to know is how common is this problem? That will help
frame whether this warrants syntax or just providing a sentinel in some
module in the stdlib that people can use (e.g. functools.NotGiven; I do
prefer MAL's naming of the sentinel). Sticking it into a module would help
minimize people from using it in places where None is entirely acceptable
and not confusing the whole community when people suddenly start peppering
their code with NotGiven instead of None for default values.
And if this is really common enough to warrant syntax, then I would want:
def foo(a, b, opt?): ...
to represent that 'opt' is optional and if not provided by the user then it
is given the value of NotGiven (or None if we are just after a syntactic
shortcut to say "this argument is optional").
So to me, there's actually two things being discussed. Do we need another
sentinel to handle the "None is valid" case, and do we want syntax to more
clearly delineate optional arguments?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170302/b75c35a6/attachment.html>
More information about the Python-ideas
mailing list