[Python-ideas] Allow __len__ to return infinity
Steven D'Aprano
steve at pearwood.info
Wed Feb 26 01:18:14 CET 2014
On Wed, Feb 26, 2014 at 12:40:59PM +1300, Greg Ewing wrote:
> Steven D'Aprano 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,
>
> What about things other than sequences?
>
> For a vector type, for example, it would make sense
> for len(v) to return the magnitude of the vector.
I don't think it does. Despite the similarities in names, I don't think
Python the language should conflate *length of a sequence or mapping*
with *arbitrary measures of length*. len(x) by historical precedent
returns the length of a sequence/mapping/set, where the result returned
is a non-negative integer value. If you want some other definition of
length, such as the length of a vowel or the length of a polynomial, or
some other metric such as Manhattan distance or Chebyshev distance, or
even vector magnitude, you can create your own function or method and
call it (say) length().
--
Steven
More information about the Python-ideas
mailing list