[Python-ideas] Optional parameters without default value

Stephan Houben stephanh42 at gmail.com
Thu Mar 2 05:22:00 EST 2017


In cases like this I would recommend creating the sentinel yourself:

NoDefault = object()

def get(store, key, default=NoDefault):
   if default is NoDefault:
        # do something

You can arrange to not export NoDefault so that the client code cannot even
access
the sentinel value.

This is strictly preferable over having yet another global
value meaning "no value", since that just moves the goal posts:
clients will complain they cannot pass in a default=NoDefault and get back
NoDefault.

Stephan


2017-03-02 11:04 GMT+01:00 M.-A. Lemburg <mal at egenix.com>:

> On 02.03.2017 10:06, Serhiy Storchaka wrote:
> > On 02.03.17 10:36, M.-A. Lemburg wrote:
> >> Why a new syntax ? Can't we just have a pre-defined sentinel
> >> singleton NoDefault and use that throughout the code (and also
> >> special case it in argument parsing/handling)?
> >>
> >> def get(store, key, default=NoDefault):
> >>     if store.exists(key):
> >>         return store.retrieve(key)
> >>     ...
> >
> > This means adding a new syntax. NoDefault should be a keyword (we can
> > reuse existing keyword couldn't be used in expression), and it should be
> > accepted only in the specific context of declaring function parameter.
>
> This is not new syntax, nor is it a keyword. It's only a
> new singleton and it is well usable outside of function
> declarations as well, e.g. for class attributes which are
> not yet initialized (and which can accept None as value).
>
> The only special casing would be in function call
> parameter parsing to signal errors when the parameter
> is used as keyword parameter.
>
> --
> Marc-Andre Lemburg
> eGenix.com
>
> Professional Python Services directly from the Experts (#1, Mar 02 2017)
> >>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
> >>> Python Database Interfaces ...           http://products.egenix.com/
> >>> Plone/Zope Database Interfaces ...           http://zope.egenix.com/
> ________________________________________________________________________
>
> ::: We implement business ideas - efficiently in both time and costs :::
>
>    eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
>     D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
>            Registered at Amtsgericht Duesseldorf: HRB 46611
>                http://www.egenix.com/company/contact/
>                       http://www.malemburg.com/
>
> _______________________________________________
> 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/5d674a09/attachment.html>


More information about the Python-ideas mailing list