NaN Vs None

François Pinard pinard at iro.umontreal.ca
Fri Sep 23 18:15:29 CEST 2005


[Peck, Jon]

> In choosing a way to represent a value of "no information" for a
> float, would it be better to use NaN or None?  None has the advantage
> of standard behavior across platforms, but NaN seems to propagate more
> easily – at least on Windows.  [...]

What I do for these things is creating a Missing type which behaves like
a number, that is, it has all __FUNCTION__ magic required by numbers.

I then create either a single missing constant or very few such missing
constants, in which case each one representing a particular way or
flavor of a missing number (unknown, not applicable, uncomputable, etc.)
The magic functions for Missing should know how to combine the few
missing constants with numbers, and with other missing constants, for
returning the "proper" one.  It's easier with only one missing constant.

You also need to take care of comparisons and logical operators on
missing values.  My usual convention is that missing values propagate
within booleans and behave like False when tested.

Sometimes, I also need "missing" strings.  That's more difficult to
implement, because of the numerous string methods and ramifications.

-- 
François Pinard   http://pinard.progiciels-bpi.ca



More information about the Python-list mailing list