[issue11674] list(obj), tuple(obj) swallow TypeError (in _PyObject_LengthHint)

Raymond Hettinger report at bugs.python.org
Fri Mar 25 23:23:27 CET 2011


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

A certain amount of exception masking is inherent is Python's design.  We use TypeError for a lot of things, including the exception raised by "len(obj)" when obj doesn't have length.

It may be possible to replace the TypeError check with test to set if __len__ is defined, but that would subtly change the semantics.  For example, if a class defined __len__ to raise a TypeError to indicate that the length is unknown (I've seen code like this in the wild being used to distinguish between finite inputs and potentially infinite inputs).

I don't really like the current design of __length_hint__, but it has been around for years and is somewhat set in stone.  If something does get changed, it should only be in Py3.3 so we don't break code that relies on the current behaviors.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11674>
_______________________________________


More information about the Python-bugs-list mailing list