[Python-ideas] `numbers.Natural`

Andrew Barnert abarnert at yahoo.com
Fri Sep 26 20:47:16 CEST 2014


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 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/7acb7352/attachment-0001.html>


More information about the Python-ideas mailing list