A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2018-June/153978.html below:

[Python-Dev] PySequence_Check but no __len__

[Python-Dev] PySequence_Check but no __len__Nick Coghlan ncoghlan at gmail.com
Fri Jun 22 08:53:47 EDT 2018
On 22 June 2018 at 21:45, Christian Tismer <tismer at stackless.com> wrote:
> Answering myself:
>
> PySequence_Check determines a sequence. See the docs.
>
> len() can but does not have to exist.
> The size is always limited.

Just to throw a couple of extra wrinkles on this:

Due to a C API implementation detail in CPython, not only can len()
throw TypeError for non-finite sequences (which implement other parts
of the sequence API, but not that), but sufficiently large finite
sequences may also throw OverflowError:

>>> data = range(-2**64, 2**64)
>>> format((data.stop - data.start) // data.step, "e")
'3.689349e+19'
>>> format(sys.maxsize, "e")
'9.223372e+18'
>>> len(data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C ssize_t
>>> data.__len__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C ssize_t

Infinite sequences that want to prevent infinite loops or unbounded
memory consumption in consumers may also choose to implement a
__length_hint__ that throws TypeError (see
https://bugs.python.org/issue33939 for a proposal to do that in
itertools).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4