[Python-ideas] Optional parameters without default value

Abe Dillon abedillon at gmail.com
Thu Mar 2 17:58:54 EST 2017


I honestly don't understand the reasoning behind using anything more
complex than a built-in sentinel value. Just plop "NotGiven" or whatever in
the built-ins and say "it's like None, but for the specific case of
optional parameters with no default value". Why prohibit people from
passing it to functions? That would just be an explicit way of saying: "I'm
not giving you a value for this parameter". Anything more than that is just
paranoia that people won't know how to use it in an expected manner.

I'm -0.5 on this proposal. It seems like it would add more confusion for
dubious benefit.

On Thu, Mar 2, 2017 at 2:07 PM, MRAB <python at mrabarnett.plus.com> wrote:

> On 2017-03-02 08:03, Serhiy Storchaka wrote:
>
>> Function implemented in Python can have optional parameters with default
>> value. It also can accept arbitrary number of positional and keyword
>> arguments if use var-positional or var-keyword parameters (*args and
>> **kwargs). But there is no way to declare an optional parameter that
>> don't have default value. Currently you need to use the sentinel idiom
>> for implementing this:
>>
>> _sentinel = object()
>> def get(store, key, default=_sentinel):
>>      if store.exists(key):
>>          return store.retrieve(key)
>>      if default is _sentinel:
>>          raise LookupError
>>      else:
>>          return default
>>
>> There are drawback of this:
>>
>> * Module's namespace is polluted with sentinel's variables.
>>
>> * You need to check for the sentinel before passing it to other function
>> by accident.
>>
>> * Possible name conflicts between sentinels for different functions of
>> the same module.
>>
>> * Since the sentinel is accessible outside of the function, it possible
>> to pass it to the function.
>>
>> * help() of the function shows reprs of default values. "foo(bar=<object
>> object at 0xb713c698>)" looks ugly.
>>
>>
>> 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".
>>
>> Example:
>>
>> def get(store, key, ?default):
>>      if store.exists(key):
>>          return store.retrieve(key)
>>      try:
>>          return default
>>      except NameError:
>>          raise LookupError
>>
>> Alternative syntaxes:
>>
>> * "=" not followed by an expression: "def get(store, key, default=)".
>>
>> * The "del" keyword: "def get(store, key, del default)".
>>
>> This feature is orthogonal to supporting positional-only parameters.
>> Optional parameters without default value can be positional-or-keyword,
>> keyword-only or positional-only (if the latter is implemented).
>>
>> Could you use 'pass' as the pseudo-sentinel?
>
> Maybe also allow "<name> is pass"/"<name> is not pass" as tests for
> absence/presence.
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170302/6f8115c8/attachment.html>


More information about the Python-ideas mailing list