[Python-ideas] Allow __len__ to return infinity

Antony Lee antony.lee at berkeley.edu
Tue Feb 25 18:35:52 CET 2014


This is similar to another idea I had some time ago: add keyword-argument
support for range, and let range(start=x, [step=y]) (stop being equal to
None) map to itertools.count(start=x, [step=y]).  No need for inf the way.


2014-02-25 4:11 GMT-08:00 Ram Rachum <ram at rachum.com>:

> This actually makes me think that `range(int('inf'))` is a more elegant
> construct than `itertools.count()`. Also `range(x, int('inf'))`
> for `itertools.count(x)`, and then you have `range(x, int('inf'), y)`
> or `range(0, int('-inf'), -1)` which `itertools.count` can't cover.
>
> On Tue, Feb 25, 2014 at 1:46 PM, Paul Moore <p.f.moore at gmail.com> wrote:
>
>> On 25 February 2014 11:18, Steven D'Aprano <steve at pearwood.info> wrote:
>> > (1) __len__ can return *any* float, regardless of value, including
>> > lengths of 0.5, NAN, 1e300, etc. This is undesirable because lengths of
>> > sequences should be positive or zero whole numbers, not arbitrary
>> > floating point values like 4.5.
>> >
>> > (2) __len__ cannot return any float, but only INF. Which means that the
>> > condition that len() only returns ints will be broken in the most
>> > surprising way, with a single exception.
>>
>> Either of these would break range(len(x)) at a minimum, and probably
>> most other code that uses len(). So you'd be left with code having to
>> identify "objects that only return finite len" and "objects that could
>> have one of the new len values". And if the OP can detect that, he can
>> just do so in his code and special case his objects, rather than
>> changing the len protocol.
>>
>> Paul
>> _______________________________________________
>> 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/
>>
>> --
>>
>> ---
>> You received this message because you are subscribed to a topic in the
>> Google Groups "python-ideas" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/python-ideas/nFYbEpHrjlk/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> python-ideas+unsubscribe at googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
> _______________________________________________
> 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/20140225/bcaa2754/attachment.html>


More information about the Python-ideas mailing list