Checking default arguments

Gabriel Genellina gagsl-py at yahoo.com.ar
Fri Feb 2 15:56:47 EST 2007


En Fri, 02 Feb 2007 15:30:53 -0300, Igor V. Rafienko <igorr at ifi.uio.no>  
escribió:

>> I was wondering whether it was possible to find out which parameter
> value is being used: the default argument or the user-supplied one.
> That is:
>
> def foo(x, y="bar"):
>     # how to figure out whether the value of y is
>     # the default argument, or user-supplied?
>
> foo(1, "bar") => user-supplied
> foo(1)        => default

You can use None as a flag:

def foo(x, y=None):
     if y is None: y="bar"
     ...

If None is valid, use your own flag:

_marker=object()
def foo(x, y=_marker):
     if y is _marker: y="bar"
     ...

-- 
Gabriel Genellina




More information about the Python-list mailing list