Functools Defaults (was Python-ideas parameter omit)
-----Original Message----- From: Steven Bethard [mailto:steven.bethard@gmail.com] Sent: Tuesday, May 15, 2007 1:54 AM
On 5/15/07, Aaron Brady <castironpi@comcast.net> wrote:
You might be able to get away without a PEP, but you'll definitely need to post an implementation patch to the bug tracker (http://sourceforge.net/tracker/?group_id=5470&atid=105470). Once you've posted your implementation, you should send an email to python-dev asking folks what they think about it. Be sure to give some code examples that using this decorator would simplify.
Code with proposal are in SourceForge [ 1719222 ] new functools. Python feature Functools gains a new decorator. `Defaults' allows its caller to placehold non-None defaults; it becomes unnecessary to know the value a place defaults to. 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?
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@gmail.com | Brisbane, Australia --------------------------------------------------------------- http://www.boredomandlaziness.org
participants (2)
-
Aaron Brady
-
Nick Coghlan