>> Iterators can implement a method called __length_hint__ that provides a hint >> to certain internal routines (such as list.extend) so they can operate more >> efficiently. As far as I can tell, __length_hint__ is currently >> undocumented. Should it be? > > This has been discussed, and no, it is a implementation detail mostly > for the optimization of builtin iterators. Right. That matches my vague recollection on the subject. >> If so, are there any constraints on what an iterator should return? I can >> think of 3 possible rules, each with advantages and disadvantages: >> 1. return your best guess Yes. BTW, the same rule also applies to __len__. IIRC, Tim proposed to add that to the docs somewhere. > Perhaps Raymond can shed some light on these. Can't guess the future of __length_hint__(). Since it doesn't have a slot, the attribute lookup can actually slow down cases with a small number of iterands. The original idea was based on some research on map/fold operations, noting that iterators can sometimes be processed more efficiently if accompanied by some metadata (i.e. the iterator has a known length, consists of unique items, is sorted, is all of a certain type, is re-iterable, etc.). Raymond
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