arigo at tunes.org
Wed Feb 8 15:20:34 CET 2006
Last september, the __len__ method of iterators was removed -- see
It was replaced by an optional undocumented method called _length_cue(),
which would be used to guess the number of remaining items in an
iterator, for performance reasons.
I'm worried about the name. There are now exactly two names that behave
like a special method without having the double-underscores around it.
The first name is 'next', which is kind of fine because it's for
iterator classes only and it's documented. But now, consider: the
CPython implementation can unexpectedly invoke a method on a
user-defined iterator class, even though this method's name is not
'__*__' and not documented as special! That's new and that's bad.
IMHO for safety reasons we need to stick double-underscores around this
name too, e.g. __length_cue__(). It's new in 2.5 and not documented
anyway so this change won't break anything. Do you agree with that?
BTW the reason I'm looking at this is that I'm considering adding
another undocumented internal-use-only method, maybe __getitem_cue__(),
that would try to guess what the nth item to be returned will be. This
would allow the repr of some iterators to display more helpful
information when playing around with them at the prompt, e.g.:
>>> enumerate([3.1, 3.14, 3.141, 3.1415, 3.14159, 3.141596])
<enumerate (0, 3.1), (1, 3.14), (2, 3.141),... length 6>
More information about the Python-Dev