[Python-ideas] `numbers.Natural`

Andrew Barnert abarnert at yahoo.com
Fri Sep 26 20:33:16 CEST 2014


On Sep 26, 2014, at 10:54, Ram Rachum <ram at rachum.com> wrote:

> I wish the `numbers` module would include a `Natural` class that would simply check whether the number is integral and positive.

That's easy to add yourself--and that means nobody else has to be involved in deciding whether "positive" or "nonnegative" is the one true definition for "natural", since you can decide on a per-project basis.

Also, it's kind of a strange check. Normally ABCs are used to check a value's _type_, not its _value_. For example, I don't think Integral considers 1.0 an integer, so why should Natural consider it a natural number? It makes more sense to create a concrete natural-number type, which does the appropriate thing in cases like subtraction underflow (is the appropriate thing returning an int instead? raising? depends on your application...), then just make Natural a normal ABC which returns true for any instance of anything that subclasses or registers with Natural, and false for anything else.


More information about the Python-ideas mailing list