[Python-ideas] optional parameters

Giampaolo Rodola' g.rodola at gmail.com
Wed Apr 16 21:55:48 CEST 2014


On Wed, Apr 16, 2014 at 9:45 PM, Yury Selivanov <yselivanov.ml at gmail.com>wrote:

>
> 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.


...but it would be yet another thing to learn.
Plus, being it uncommon you would easily forget what's the "right way to do
it" and likely come up with the usual object() hack anyway.


-- 
Giampaolo - http://grodola.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140416/9982ee13/attachment.html>


More information about the Python-ideas mailing list