[Python-ideas] `numbers.Natural`

Thomas Gläßle t_glaessle at gmx.de
Fri Sep 26 22:54:08 CEST 2014


David Mertz wrote on 09/26/2014 10:38 PM:
> On Fri, Sep 26, 2014 at 1:27 PM, Ian Cordasco
> <graffatcolmingov at gmail.com <mailto:graffatcolmingov at gmail.com>> 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
>
>
> This code seems a bit broken, given:
>
> >>> issubclass(numbers.Rational, numbers.Real)
> Out[3]: True
>  
>
>     def isNatural(x):
>         return isinstance(x, int) and x > 0
>
>
> I think the original desired behavior is this different one-liner:
>
>     isNatural = lambda x: int(x)==x and x>0

Not sure about the original desired behaviour. I would have guessed the
former function as well, as it more closely resembles the isinstance
checks with the numbers types:

    >>> isinstance(1.0, (numbers.Rational, numbers.Integral))
    False

(one might argue, that 1.0 is a rational value, as is true for most
float values, but the type has a different intention)



>
>
> -- 
> Keeping medicines from the bloodstreams of the sick; food
> from the bellies of the hungry; books from the hands of the
> uneducated; technology from the underdeveloped; and putting
> advocates of freedom in prisons.  Intellectual property is
> to the 21st century what the slave trade was to the 16th.
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140926/48d65fd1/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 949 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140926/48d65fd1/attachment.sig>


More information about the Python-ideas mailing list