I wish the `numbers` module would include a `Natural` class that would simply check whether the number is integral and positive.
At first glance it sounds nice and straight forward. But - is a natural positive, or just non-negative? I guess, I'd have to look it up in the docs each time, since either definition is used in lots of places. Also, Natural does not correspond to the python type, but its value. So, you couldn't use it with issubclass. Ram Rachum wrote on 09/26/2014 07:54 PM:
I wish the `numbers` module would include a `Natural` class that would simply check whether the number is integral and positive.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
I agree with both the points you raised, they're both disadvantages. The
question is whether the uses would be worth these two disadvantages.
(`collections.Hashable` also has the second disadvantage you mentioned, and
it's still in the stdlib, so there's hope.)
On Fri, Sep 26, 2014 at 9:10 PM, Thomas Gläßle
At first glance it sounds nice and straight forward. But - is a natural positive, or just non-negative? I guess, I'd have to look it up in the docs each time, since either definition is used in lots of places. Also, Natural does not correspond to the python type, but its value. So, you couldn't use it with issubclass.
Ram Rachum wrote on 09/26/2014 07:54 PM:
I wish the `numbers` module would include a `Natural` class that would simply check whether the number is integral and positive.
_______________________________________________ Python-ideas mailing listPython-ideas@python.orghttps://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On Sep 26, 2014, at 11:13, Ram Rachum
I agree with both the points you raised, they're both disadvantages. The question is whether the uses would be worth these two disadvantages. (`collections.Hashable` also has the second disadvantage you mentioned, and it's still in the stdlib, so there's hope.)
Hashable doesn't have that disadvantage. It checks whether the object's class or any superclass has a __hash__ method. So it's still based on the type, not on the value, and it works as expected with issubclass.
On Fri, Sep 26, 2014 at 9:10 PM, Thomas Gläßle
wrote: At first glance it sounds nice and straight forward. But - is a natural positive, or just non-negative? I guess, I'd have to look it up in the docs each time, since either definition is used in lots of places. Also, Natural does not correspond to the python type, but its value. So, you couldn't use it with issubclass.
Ram Rachum wrote on 09/26/2014 07:54 PM:
I wish the `numbers` module would include a `Natural` class that would simply check whether the number is integral and positive.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
I checked it and you're right. So I guess `Hashable` is a bit confusing:
>>> isinstance(([3],), collections.Hashable)
True
On Fri, Sep 26, 2014 at 9:35 PM, Andrew Barnert
On Sep 26, 2014, at 11:13, Ram Rachum
wrote: I agree with both the points you raised, they're both disadvantages. The question is whether the uses would be worth these two disadvantages. (`collections.Hashable` also has the second disadvantage you mentioned, and it's still in the stdlib, so there's hope.)
Hashable doesn't have that disadvantage. It checks whether the object's class or any superclass has a __hash__ method. So it's still based on the type, not on the value, and it works as expected with issubclass.
On Fri, Sep 26, 2014 at 9:10 PM, Thomas Gläßle
wrote: At first glance it sounds nice and straight forward. But - is a natural positive, or just non-negative? I guess, I'd have to look it up in the docs each time, since either definition is used in lots of places. Also, Natural does not correspond to the python type, but its value. So, you couldn't use it with issubclass.
Ram Rachum wrote on 09/26/2014 07:54 PM:
I wish the `numbers` module would include a `Natural` class that would simply check whether the number is integral and positive.
_______________________________________________ Python-ideas mailing listPython-ideas@python.orghttps://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On Sep 26, 2014, at 11:37, Ram Rachum
I checked it and you're right. So I guess `Hashable` is a bit confusing:
>>> isinstance(([3],), collections.Hashable) True
It makes sense if you think of isinstance as a type check, which is what it's supposed to be, rather than a value check. ([3],) is a tuple, and tuples are hashable as a type, even though some specific tuple values might not be. That's exactly why I think you want a type that you can check for Natural, rather than something about the value. That's what isinstance is for, and if you start subverting it to mean other things, that's when it leads to confusion.
On Fri, Sep 26, 2014 at 9:35 PM, Andrew Barnert
wrote: On Sep 26, 2014, at 11:13, Ram Rachum
wrote: I agree with both the points you raised, they're both disadvantages. The question is whether the uses would be worth these two disadvantages. (`collections.Hashable` also has the second disadvantage you mentioned, and it's still in the stdlib, so there's hope.)
Hashable doesn't have that disadvantage. It checks whether the object's class or any superclass has a __hash__ method. So it's still based on the type, not on the value, and it works as expected with issubclass.
On Fri, Sep 26, 2014 at 9:10 PM, Thomas Gläßle
wrote: At first glance it sounds nice and straight forward. But - is a natural positive, or just non-negative? I guess, I'd have to look it up in the docs each time, since either definition is used in lots of places. Also, Natural does not correspond to the python type, but its value. So, you couldn't use it with issubclass.
Ram Rachum wrote on 09/26/2014 07:54 PM:
I wish the `numbers` module would include a `Natural` class that would simply check whether the number is integral and positive.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Isn't the right answer to create an isNatural() function? Why do we need a type or class rather than just a function? It's not any easier to write 'isinstance(x, Natural)' than it is to write 'isNatural(x)'. On Fri, Sep 26, 2014 at 11:47 AM, Andrew Barnert < abarnert@yahoo.com.dmarc.invalid> wrote:
On Sep 26, 2014, at 11:37, Ram Rachum
wrote: I checked it and you're right. So I guess `Hashable` is a bit confusing:
>>> isinstance(([3],), collections.Hashable) True
It makes sense if you think of isinstance as a type check, which is what it's supposed to be, rather than a value check. ([3],) is a tuple, and tuples are hashable as a type, even though some specific tuple values might not be.
That's exactly why I think you want a type that you can check for Natural, rather than something about the value. That's what isinstance is for, and if you start subverting it to mean other things, that's when it leads to confusion.
On Fri, Sep 26, 2014 at 9:35 PM, Andrew Barnert
wrote: On Sep 26, 2014, at 11:13, Ram Rachum
wrote: I agree with both the points you raised, they're both disadvantages. The question is whether the uses would be worth these two disadvantages. (`collections.Hashable` also has the second disadvantage you mentioned, and it's still in the stdlib, so there's hope.)
Hashable doesn't have that disadvantage. It checks whether the object's class or any superclass has a __hash__ method. So it's still based on the type, not on the value, and it works as expected with issubclass.
On Fri, Sep 26, 2014 at 9:10 PM, Thomas Gläßle
wrote: At first glance it sounds nice and straight forward. But - is a natural positive, or just non-negative? I guess, I'd have to look it up in the docs each time, since either definition is used in lots of places. Also, Natural does not correspond to the python type, but its value. So, you couldn't use it with issubclass.
Ram Rachum wrote on 09/26/2014 07:54 PM:
I wish the `numbers` module would include a `Natural` class that would simply check whether the number is integral and positive.
_______________________________________________ Python-ideas mailing listPython-ideas@python.orghttps://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- 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.
Hmm, I don't know, but I like it better. I probably had a reason and forgot
it. Sorry :(
On Fri, Sep 26, 2014 at 11:03 PM, David Mertz
Isn't the right answer to create an isNatural() function? Why do we need a type or class rather than just a function? It's not any easier to write 'isinstance(x, Natural)' than it is to write 'isNatural(x)'.
On Fri, Sep 26, 2014 at 11:47 AM, Andrew Barnert < abarnert@yahoo.com.dmarc.invalid> wrote:
On Sep 26, 2014, at 11:37, Ram Rachum
wrote: I checked it and you're right. So I guess `Hashable` is a bit confusing:
>>> isinstance(([3],), collections.Hashable) True
It makes sense if you think of isinstance as a type check, which is what it's supposed to be, rather than a value check. ([3],) is a tuple, and tuples are hashable as a type, even though some specific tuple values might not be.
That's exactly why I think you want a type that you can check for Natural, rather than something about the value. That's what isinstance is for, and if you start subverting it to mean other things, that's when it leads to confusion.
On Fri, Sep 26, 2014 at 9:35 PM, Andrew Barnert
wrote: On Sep 26, 2014, at 11:13, Ram Rachum
wrote: I agree with both the points you raised, they're both disadvantages. The question is whether the uses would be worth these two disadvantages. (`collections.Hashable` also has the second disadvantage you mentioned, and it's still in the stdlib, so there's hope.)
Hashable doesn't have that disadvantage. It checks whether the object's class or any superclass has a __hash__ method. So it's still based on the type, not on the value, and it works as expected with issubclass.
On Fri, Sep 26, 2014 at 9:10 PM, Thomas Gläßle
wrote: At first glance it sounds nice and straight forward. But - is a natural positive, or just non-negative? I guess, I'd have to look it up in the docs each time, since either definition is used in lots of places. Also, Natural does not correspond to the python type, but its value. So, you couldn't use it with issubclass.
Ram Rachum wrote on 09/26/2014 07:54 PM:
I wish the `numbers` module would include a `Natural` class that would simply check whether the number is integral and positive.
_______________________________________________ Python-ideas mailing listPython-ideas@python.orghttps://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- 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.
On Fri, Sep 26, 2014 at 3:06 PM, Ram Rachum
Hmm, I don't know, but I like it better. I probably had a reason and forgot it. Sorry :(
On Fri, Sep 26, 2014 at 11:03 PM, David Mertz
wrote: Isn't the right answer to create an isNatural() function? Why do we need a type or class rather than just a function? It's not any easier to write 'isinstance(x, Natural)' than it is to write 'isNatural(x)'.
On Fri, Sep 26, 2014 at 11:47 AM, Andrew Barnert
wrote: On Sep 26, 2014, at 11:37, Ram Rachum
wrote: I checked it and you're right. So I guess `Hashable` is a bit confusing:
>>> isinstance(([3],), collections.Hashable) True
It makes sense if you think of isinstance as a type check, which is what it's supposed to be, rather than a value check. ([3],) is a tuple, and tuples are hashable as a type, even though some specific tuple values might not be.
That's exactly why I think you want a type that you can check for Natural, rather than something about the value. That's what isinstance is for, and if you start subverting it to mean other things, that's when it leads to confusion.
On Fri, Sep 26, 2014 at 9:35 PM, Andrew Barnert
wrote: On Sep 26, 2014, at 11:13, Ram Rachum
wrote: I agree with both the points you raised, they're both disadvantages. The question is whether the uses would be worth these two disadvantages. (`collections.Hashable` also has the second disadvantage you mentioned, and it's still in the stdlib, so there's hope.)
Hashable doesn't have that disadvantage. It checks whether the object's class or any superclass has a __hash__ method. So it's still based on the type, not on the value, and it works as expected with issubclass.
On Fri, Sep 26, 2014 at 9:10 PM, Thomas Gläßle
wrote: At first glance it sounds nice and straight forward. But - is a natural positive, or just non-negative? I guess, I'd have to look it up in the docs each time, since either definition is used in lots of places. Also, Natural does not correspond to the python type, but its value. So, you couldn't use it with issubclass.
Ram Rachum wrote on 09/26/2014 07:54 PM:
I wish the `numbers` module would include a `Natural` class that would simply check whether the number is integral and positive.
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): # But really, I agree with David that *if* this were added to the stdlib, it should be as a function. I'm however, unconvinced about the usefulness of having this as a stdlib function (since it should be easy for anyone to define): def isNatural(x): if not isinstance(x, int): return False return x > 0 But here we could argue over whether 0 is Natural or not. Further, this could be rewritten as a one-liner: def isNatural(x): return isinstance(x, int) and x > 0 Which falls under the adage I've oft heard repeated here "Not every one-liner belongs in the standard library"
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~
On Sep 26, 2014, at 13:33, Ethan Furman
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...
Except that 2/3 is a float, and (-1)**.5 is a complex, so it might be perfectly reasonable to expect n5-n7 to be an int. Or maybe Natural(0) or Natural('nan') even, on analogy with 2//3 and whatever float operations return NaN values. As I said in my first message, which one makes more sense probably depends on your application, which is part of why this doesn't belong in the stdlib in the first place... But if it did belong, I'd expect it to act like int and float and underflow to an int. (In which case a type check for Natural might be sensible, but would not often be useful--just like a type check on the result of a/b for integers is sensible but not often useful.)
On Fri, Sep 26, 2014 at 1:27 PM, Ian Cordasco
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 -- 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.
isNatural = lambda x: int(x)==x and x>0
Actually, sorry. This is crashy; what I mean is: from numbers import Number isNatural = lambda x: isinstance(x, Number) and int(x)==x and x>0 -- 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.
David Mertz wrote on 09/26/2014 10:38 PM:
On Fri, Sep 26, 2014 at 1:27 PM, Ian Cordasco
mailto:graffatcolmingov@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@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Thomas Gläßle wrote on 09/26/2014 10:54 PM:
David Mertz wrote on 09/26/2014 10:38 PM:
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
And more importantly: you wouldn't expect isNatural(3.5) to return True.
On Fri, Sep 26, 2014 at 1:57 PM, Thomas Gläßle
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.
On 09/26/2014 02:04 PM, David Mertz wrote:
On Fri, Sep 26, 2014 at 1:57 PM, Thomas Gläßle
mailto:t_glaessle@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
If we had PEP 463 [1] it could still be a one-liner ;) def is_natural(x): return int(x) == x and x > 0 except TypeError, ValueError: False -- ~Ethan~ [1] http://legacy.python.org/dev/peps/pep-0463/
David Mertz
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
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)'.
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.[*]
[*] 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()'.
On Sep 26, 2014 4:42 PM, "Ben Finney"
David Mertz
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@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On Sep 26, 2014, at 17:01, David Mertz
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"
wrote: David Mertz
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@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On Sat, Sep 27, 2014 at 11:13 AM, Andrew Barnert
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.
Assuming the characters you're after are U+2208 'ELEMENT OF' and U+2115 'DOUBLE-STRUCK CAPITAL N', your name would be is∈ℕ(). I'd prefer isℕ() for two reasons: firstly, ∈ is a symbol, so it's not valid in a name (though you could open the other can of worms and ask for it to be an operator - then you could spell it "x ∈ ℕ" instead of "isℕ(x)"), and secondly because it's much more common to ask "is natural?" than "is element-of natural?" in function names. But I think this has long gone into crazyland. ChrisA
I've mentioned before that I used vim-cute-python (
https://github.com/ehamberg/vim-cute-python), but customized myself (
http://gnosis.cx/bin/.vim/after/syntax/python.vim).
I do not utilize U+2115 'DOUBLE-STRUCK CAPITAL N' anywhere, but it is an
elegant character. I use others such as:
syntax match pyNiceOperator "\
On Sat, Sep 27, 2014 at 11:13 AM, Andrew Barnert
wrote: 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.
Assuming the characters you're after are U+2208 'ELEMENT OF' and U+2115 'DOUBLE-STRUCK CAPITAL N', your name would be is∈ℕ(). I'd prefer isℕ() for two reasons: firstly, ∈ is a symbol, so it's not valid in a name (though you could open the other can of worms and ask for it to be an operator - then you could spell it "x ∈ ℕ" instead of "isℕ(x)"), and secondly because it's much more common to ask "is natural?" than "is element-of natural?" in function names. But I think this has long gone into crazyland.
ChrisA _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- 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.
On Sat, Sep 27, 2014, at 01:04, Chris Angelico wrote:
Assuming the characters you're after are U+2208 'ELEMENT OF' and U+2115 'DOUBLE-STRUCK CAPITAL N', your name would be is∈ℕ(). I'd prefer isℕ() for two reasons: firstly, ∈ is a symbol, so it's not valid in a name (though you could open the other can of worms and ask for it to be an operator - then you could spell it "x ∈ ℕ" instead of "isℕ(x)"), and secondly because it's much more common to ask "is natural?" than "is element-of natural?" in function names. But I think this has long gone into crazyland.
Speaking of the other can of worms... we already _have_ that operator, it is spelled "in". What we don't have is infinite sets.
On Sun, Sep 28, 2014 at 12:13:43AM -0400, random832@fastmail.us wrote:
On Sat, Sep 27, 2014, at 01:04, Chris Angelico wrote:
Assuming the characters you're after are U+2208 'ELEMENT OF' and U+2115 'DOUBLE-STRUCK CAPITAL N', your name would be is∈ℕ(). [...] Speaking of the other can of worms... we already _have_ that operator, it is spelled "in". What we don't have is infinite sets.
Guys, a reminder please: Python is a general purpose programming language with a general-purpose notation, not Mathematica. The most specialised the task, or the notation, the less likely it is to belong in Python the language or the standard library. But feel free to create your own libraries, or even your own parser for a mini-language capable of interpreting things like x ∈ ℕ. Or help contribute to Sage. http://www.sagemath.org -- Steven
On Sep 26, 2014, at 10:54, Ram Rachum
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.
participants (10)
-
Andrew Barnert
-
Ben Finney
-
Chris Angelico
-
David Mertz
-
Ethan Furman
-
Ian Cordasco
-
Ram Rachum
-
random832@fastmail.us
-
Steven D'Aprano
-
Thomas Gläßle