Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82394

Re: How to detect that a function argument is the default one

From Ben Finney <ben+python@benfinney.id.au>
Subject Re: How to detect that a function argument is the default one
Date 2014-12-12 10:03 +1100
References <5488634e$0$2130$426a74cc@news.free.fr> <5489f45d$0$53762$c3e8da3$3304c218@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.16879.1418339026.18130.python-list@python.org> (permalink)

Show all headers | View raw


Tony the Tiger <tony@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

Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

How to detect that a function argument is the default one "ast" <nomail@invalid.com> - 2014-12-10 16:14 +0100
  Re: How to detect that a function argument is the default one Skip Montanaro <skip.montanaro@gmail.com> - 2014-12-10 09:29 -0600
    Re: How to detect that a function argument is the default one "ast" <nomail@invalid.com> - 2014-12-10 16:45 +0100
    Re: How to detect that a function argument is the default one Marko Rauhamaa <marko@pacujo.net> - 2014-12-10 18:33 +0200
  Re: How to detect that a function argument is the default one Chris Angelico <rosuav@gmail.com> - 2014-12-11 03:16 +1100
    Re: How to detect that a function argument is the default one wxjmfauth@gmail.com - 2014-12-10 12:08 -0800
    Re: How to detect that a function argument is the default one "ast" <nomail@invalid.com> - 2014-12-10 22:17 +0100
      Re: How to detect that a function argument is the default one Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-11 11:03 -0700
      Re: How to detect that a function argument is the default one Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-11 11:07 -0700
  Re: How to detect that a function argument is the default one Ned Batchelder <ned@nedbatchelder.com> - 2014-12-10 11:19 -0500
  Re: How to detect that a function argument is the default one Ian Kelly <ian.g.kelly@gmail.com> - 2014-12-10 09:20 -0700
  Re: How to detect that a function argument is the default one Tony the Tiger <tony@tiger.invalid> - 2014-12-11 19:45 +0000
    Re: How to detect that a function argument is the default one Ben Finney <ben+python@benfinney.id.au> - 2014-12-12 10:03 +1100

csiph-web