How to detect that a function argument is the default one

Ben Finney ben+python at benfinney.id.au
Thu Dec 11 18:03:28 EST 2014


Tony the Tiger <tony at tiger.invalid> writes:

> radius='10', mass='1'
>
> if radius == '10' ...
>
> if mass == '1' ...

This ignores the problem as stated: The OP wants to distinguish between
a value that was explicitly set by the caller, versus a value that was
set by default because the caller did not specify the parameter.

The general solution to this is to choose a value outside the domain, a
<URL:https://en.wikipedia.org/wiki/Sentinel_value> which is different
from any value the caller would choose for that parameter.

Often, the Python singleton ‘None’ is good for the purpose. I think in
this case, where the domain is numeric, a default of ‘None’ would do
fine.

    class Foo:
        def __init__(centre=(0, 0), radius=None, mass=None):
            if radius is None:
                # Caller didn't specify. Set a default value.
                radius = 10
            if mass is None:
                # Caller didn't specify. Set a default value.
                mass = 1
            self.centre = centre
            self.radius = radius
            self.mass = mass

In some other cases, ‘None’ is a valid value for the caller to set, and
so cannot be used as a sentinel to distinguish the default. For those
cases, creating a specific object instance to serve as the sentinel is
simple and clear:

    class Foo:

        _SENTINEL = object()

        def __init__(centre=(0, 0), radius=_SENTINEL, mass=_SENTINEL):
            if radius is _SENTINEL:
                # Caller didn't specify. Set a default value.
                radius = 10
            if mass is _SENTINEL:
                # Caller didn't specify. Set a default value.
                mass = 1
            self.centre = centre
            self.radius = radius
            self.mass = mass

Creating a new object specifically for the sentinel means there is no
chance some other value will compare identical. Choosing a
leading-underscore name indicates to the reader that this is not part of
the public API.

-- 
 \        “Odious ideas are not entitled to hide from criticism behind |
  `\       the human shield of their believers' feelings.” —Richard M. |
_o__)                                                         Stallman |
Ben Finney




More information about the Python-list mailing list