[Python-ideas] `numbers.Natural`

David Mertz mertz at gnosis.cx
Fri Sep 26 22:03:02 CEST 2014


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 at yahoo.com.dmarc.invalid> wrote:

> On Sep 26, 2014, at 11:37, Ram Rachum <ram at rachum.com> 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 <abarnert at yahoo.com>
> wrote:
>
>> On Sep 26, 2014, at 11:13, Ram Rachum <ram at rachum.com> 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 <t_glaessle at gmx.de> 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 at python.orghttps://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/
>>
>>
>
> _______________________________________________
> 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/
>



-- 
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/59ab9d23/attachment.html>


More information about the Python-ideas mailing list