[Python-ideas] optional parameters
Yury Selivanov
yselivanov.ml at gmail.com
Wed Apr 16 21:45:43 CEST 2014
On 2014-04-16, 1:29 PM, Guido van Rossum 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)
Agree, wrapping is hard, but you can always use **kwargs.
The pattern is sometimes useful for decorators (as Ryan Gonzalez
shows in this thread). But basically, you need this when 'None'
is a legitimate value for some optional argument, and you want
users to pass explicitly.
For instance, see inspect.Signature.replace method:
http://hg.python.org/cpython/file/e4ee0b15cc4f/Lib/inspect.py#l2193
I'm not saying it's a very common thing to do, but sometimes
you need this, and having the need of doing some ad hoc hacks
is slightly annoying.
Yury
More information about the Python-ideas
mailing list