[Python-ideas] parameter omit

Ron Adam rrr at ronadam.com
Mon May 14 01:57:24 CEST 2007


Aaron Brady wrote:
> Is it possible to signal to a parameter to use its default?
>  
> 
> def f( a, b=None, c=’’ ):...
> 
> f( 123, <def>, ‘abc’ )
> 
> f( 123, ObjA, <def> )
> 
>   
> Did I miss something, plus did you cover it before?

You might be able to make a decorator that handles this the way you describe.


It might look something like the following.


class DefaultValue(object):
    """  An signal object. """
    pass
DfV = DefaultValue()    # Default value signal object


def defaults(*args):
    " a decorator that assigns and checks defaults values."
    ...


@defaults(None, None, '')
def f(a, b, c):
    return a, b, c


Then you could do as you wrote above and get something like...

 >>> f(123, DfV, 'abc')
123, None, 'abc'

 >>> f(123, ObjA, DfV)
123, <OjbA>, ''


I'll leave the actual implementation to you.


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.

Cheers,
    Ron











More information about the Python-ideas mailing list