None versus MISSING sentinel -- request for design feedback

bruno.desthuilliers at gmail.com bruno.desthuilliers at gmail.com
Fri Jul 15 09:59:17 CEST 2011


On Jul 15, 7:28 am, Steven D'Aprano <steve
+comp.lang.pyt... at pearwood.info> wrote:
>
> I'm designing an API for some lightweight calculator-like statistics
> functions, such as mean, standard deviation, etc., and I want to support
> missing values. Missing values should be just ignored. E.g.:


(snip)

> Against None: it's too easy to mistakenly add None to a data set by mistake,
> because functions return None by default.

Yeps.

> In favour of a dedicated MISSING singleton: it's obvious from context. It's
> not a lot of work to implement compared to using None. Hard to accidentally
> include it by mistake. If None does creep into the data by accident, you
> get a nice explicit exception.
>
> Against MISSING: users may expect to be able to choose their own sentinel by
> assigning to MISSING. I don't want to support that.

What about allowing users to specificy their own sentinel in the
simplest pythonic way:

# stevencalc.py
MISSING = object()

def mean(values, missing=MISSING):
    your code here


Or, if you want to make it easier to specify the sentinel once for the
whole API:

# stevencalc.py
MISSING = object()

class Calc(object):
    def __init__(self, missing=MISSING):
        self._missing = missing
    def mean(self, values):
        # your code here


# default:
_calc = Calc()
mean = _calc.mean
# etc...

My 2 cents...



More information about the Python-list mailing list