[Python-ideas] optional parameters

Vernon D. Cole vernondcole at gmail.com
Wed Apr 16 20:20:48 CEST 2014


On Wed, Apr 16, 2014 at 10:09 AM, Yury Selivanov <yselivanov.ml at gmail.com>
wrote:


> There is a very common pattern for creating optional arguments
> when you can't use None:
>
>
>
> It's a bit annoying to create this marker objects, and also,
> if you try to render a signature of such function, you'll get
> something like:
>
> "(*, arg1='spam', arg3=None, arg4=<object object at 0x104be7080>)"
>
> What if we add a standard marker for this use-case:
> functools.optional or inspect.Parameter.optional?
>
>
There is already a singleton which works very well for this use case:

def foo(*, arg1='spam', arg3=None, arg4=NotImplemented):
     if arg4 is NotImplemented:
         # caller didn't pass *anything* for arg4
     else:
         # caller did pass some (maybe None) value for arg4

It is  already defined, and reads like sensible English.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140416/8ca71046/attachment.html>


More information about the Python-ideas mailing list