[Python-ideas] `numbers.Natural`

Andrew Barnert abarnert at yahoo.com
Sat Sep 27 03:13:53 CEST 2014


On Sep 26, 2014, at 17:01, David Mertz <mertz at gnosis.cx> wrote:

> Well, yeah. I think I lost that discussion in the type annotation thread though. I'd love types to include predicates, but Guido wouldn't 😏.  An 'is_natural()' function--by whichever spelling--needs to contain a test which isn't in the type system, whichever result one chooses for 'is_natural(1.0)'.
> 
It is (or should be) in the type system for any application which has a good use for a natural number type. That's pretty much why Python has classes in the first place.

Anyway, the only time I remember ever writing a Natural class in a language that didn't have one was to demonstrate Peano arithmetic, so in Python I'd be inheriting from or encapsulating a frozenset, not an int. I can imagine doing something similar (but more useful) in a symbolic math package. I could even see a Natural type being useful for the same kinds of things C uses unsigned for, but not if it was defined as 1.. instead of 0.. Do people have an actual use case for the type we're talking about here?
> FWIW, I want 'isNatural(0j+1) == True'. But I'm on the losing end of at least three arguments there.... Unless I just roll my own in 5-8 lines of code.[*]
> 
What about something that's one denornal bit of rounding error away from 0j+1?
> [*] I'd want the non-PEP8 spelling because "Natural" in mathematics is always capitalized.  That's why we have the built-in 'isGuido(BDFL)' rather than 'is_guido()'.
> 
Wouldn't is\N{ELEMENT}\N{DOUBLE_N} or something like that (sorry, don't know how to type the actual Unicode on my phone, do know how to look up the right names but too lazy to do so) be better if "in mathematics" is what you really want? In math textbooks and papers, the symbols are usually translated to "is a natural number", not "is a Natural number", so I don't think violating PEP 8 is warranted here.


> On Sep 26, 2014 4:42 PM, "Ben Finney" <ben+python at benfinney.id.au> wrote:
>> David Mertz <mertz at gnosis.cx> writes:
>> 
>> > Isn't the right answer to create an isNatural() function?
>> 
>> No, the right answer (by PEP 8) is to create an ‘is_natural’ or
>> ‘isnatural’ function :-)
>> 
>> > Why do we need a type or class rather than just a function?
>> 
>> Less facetiously: I suspect what is being asked for here is a type which
>> will *ensure* the values are natural numbers.
>> 
>> That is a good use of types, IMO: they enforce a domain of values.
>> 
>> I'm not convinced there is a pressing need to add such a type to the
>> standard type hierarchy though.
>> 
>> --
>>  \      “I find the whole business of religion profoundly interesting. |
>>   `\     But it does mystify me that otherwise intelligent people take |
>> _o__)                                    it seriously.” —Douglas Adams |
>> Ben Finney
>> 
>> _______________________________________________
>> 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/
> _______________________________________________
> 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/6816c111/attachment-0001.html>


More information about the Python-ideas mailing list