[Python-ideas] `numbers.Natural`
David Mertz
mertz at gnosis.cx
Fri Sep 26 23:04:39 CEST 2014
On Fri, Sep 26, 2014 at 1:57 PM, Thomas Gläßle <t_glaessle at gmx.de> wrote
> And more importantly: you wouldn't expect isNatural(3.5) to return True.
>
That's not a problem with my one-liner, nor Ian's:
>>> isNatural = lambda x: int(x)==x and x>0
>>> isNatural(3.5)
False
However, my correction to avoid crashing on some inputs is still wrong,
given:
>>> isinstance(0j, numbers.Number)
True
>>> int(0j)
TypeError
So I think I really need a couple lines:
def isNatural(x):
try:
return int(x)==x and x>0
except (TypeError, ValueError):
return False
Of course... if you don't want that behavior, you need to write a different
function :-).
--
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140926/e25b0bdb/attachment.html>
More information about the Python-ideas
mailing list