[Python-ideas] optional parameters
Ryan Gonzalez
rymg19 at gmail.com
Wed Apr 16 20:39:07 CEST 2014
On Wed, Apr 16, 2014 at 12:29 PM, Guido van Rossum <guido at python.org> wrote:
> TBH I think it's usually an anti-pattern when you have an API where the
> absence of a parameter is not equivalent to some default value. It makes
> wrapping such APIs awkward, e.g.
>
> def logging_foo(*, arg1='something_other_than_spam', arg3=None,
> arg4=<what???>):
> loging.info('calling foo(arg1=%r, arg3=%r, arg4=%r)', arg1, arg3,
> arg4)
> return foo(arg1=arg1, arg3=arg3, arg4=arg4)
>
>
I've had to do that before when writing decorators that take parameters,
e.g:
def mydecorator(arg1,arg2=None):
def _f(f):
if arg2 is None: arg2 = f.something
...
return f
return _f
Which sucks when None is a valid value.
>
> On Wed, Apr 16, 2014 at 10:09 AM, Yury Selivanov <yselivanov.ml at gmail.com>wrote:
>
>> Hello,
>>
>> There is a very common pattern for creating optional arguments
>> when you can't use None:
>>
>> _optional = object()
>> def foo(*, arg1='spam', arg3=None, arg4=_optional):
>> if arg4 is _optional:
>> # caller didn't pass *anything* for arg4
>> else:
>> # caller did pass some (maybe None) value for arg4
>>
>> 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?
>>
>>
>> Yury
>> _______________________________________________
>> 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/
>>
>
>
>
> --
> --Guido van Rossum (python.org/~guido)
>
> _______________________________________________
> 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/
>
--
Ryan
If anybody ever asks me why I prefer C++ to C, my answer will be simple:
"It's becauseslejfp23(@#Q*(E*EIdc-SEGFAULT. Wait, I don't think that was
nul-terminated."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140416/029bc8bd/attachment-0001.html>
More information about the Python-ideas
mailing list