[Python-ideas] `numbers.Natural`

Ethan Furman ethan at stoneleaf.us
Fri Sep 26 22:33:45 CEST 2014


On 09/26/2014 01:27 PM, Ian Cordasco wrote:
>
> The only motivations I can think of for making this a type (not that
> they're good ones) is you could do something like:
>
> if isinstance(x, (Natural, Complex, Real)):
>     #
> elif isinstance(x, Rational):
>     #
> else:
>     raise ValueError
>
> Which is naturally (no pun intended) slightly nicer than
>
> if isinstance(x, (Complex, Real)) or isNatural(x):
>      #

The Good Reason for having a Natural type would be not having to always check for illegal values -- if one is (attempted 
to be) created, an exception is raised on the spot.

n5 = Natural(5)
n7 = Natural(7)
n5 - n7
Traceback...

--
~Ethan~


More information about the Python-ideas mailing list