Checking default arguments
Neil Cerutti
horpner at yahoo.com
Fri Feb 2 14:14:28 EST 2007
On 2007-02-02, Igor V. Rafienko <igorr at ifi.uio.no> wrote:
> Hi,
>
> 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
>
> {}.pop seems to be able to make this dictinction.
You can fake it (this may be how dict.pop work) by not providing
defaults, but using positional arguments.
Here's a silly example, which returns a tuple if the user
supplies the second argument, and a list otherwise.
def foo(x, *args):
if len(args) == 0:
y_provided = True
y = "bar"
else:
y_provided = False
y = args[0]
if y_provided:
return (x, y)
else:
return [x, y]
--
Neil Cerutti
Wonderful bargains for men with 16 and 17 necks --sign at clothing store
More information about the Python-list
mailing list