[Python-Dev] Functools Defaults (was Python-ideas parameter omit)

Nick Coghlan ncoghlan at gmail.com
Tue May 15 15:51:59 CEST 2007


Aaron Brady wrote:
> It might be useful in cases where you want the calling signature to look
> alike for a group of dispatched functions and the added overhead the
> decorator adds isn't a problem.  But you probably wouldn't want that
> overhead all the time, so having it as an optional decorator would be good.
> -Ron Adam
> 
> What -do- you think about it?

-1

It took me a couple of rereads to actually figure out what this 
decorator was trying to do (which is simply allowing callers to skip 
parameters in a function call without using keyword arguments).

I think it would be significantly clearer (and far more efficient) to 
simply use keyword arguments at the call site where the parameters are 
being skipped rather than significantly slowing down every single call 
to a function simply to permit some sloppy coding.

To take the example from the SF tracker:

  .>>> @defaults.
  .>>> def f(a=123, b=None, c='abc'):
  .>>>   return a, b, c
  .>>>
  .>>> use_default = defaults.absent
  .>>> f(123, use_default, 'abc')
  123, None, 'abc'

As opposed to:

  .>>> def f(a=123, b=None, c='abc'):
  .>>>   return a, b, c
  .>>>
  .>>> f(123, c='abc')
  123, None, 'abc'

Keyword only parameters (coming in Py3k and possibly in 2.6) will be an 
even cleaner solution for cases where you don't want callers to have to 
care about a default value, but still give them the ability to override 
it if needed.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list