[Python-ideas] Allow __len__ to return infinity
Antoine Pitrou
solipsis at pitrou.net
Tue Feb 25 12:51:17 CET 2014
On Tue, 25 Feb 2014 02:02:15 -0800 (PST)
Ram Rachum <ram.rachum at gmail.com> wrote:
> I'd like to have some objects that return infinity from their __len__
> method. Unfortunately the __len__ method may only return an int, and it's
> impossible to represent an infinity as an int. Do you think that Python
> could allow returning infinity from __len__?
The question is what that would achieve? Consumers of len() expect it
to return a finite integer, so returning infinite would break lots of
existing code.
If you're willing to be practical, you can still return sys.maxsize,
for example (which is not far from infinite on 64-bit systems ;-)).
Regards
Antoine.
More information about the Python-ideas
mailing list