Module Expr Str Import alias ImportFrom alias ImportFrom alias ImportFrom alias ImportFrom alias identifier Assign NameStore Num FunctionDef arguments args NameParam defaults body TryExcept body Return Call NameLoad NameLoad handlers ExceptHandler type NameLoad body TryExcept body Return Call AttributeLoad NameLoad attr handlers ExceptHandler type NameLoad body Raise NameLoad decorator_list ClassDef bases body FunctionDef arguments args NameParam defaults body Assign NameStore AttributeLoad NameLoad attr For NameStore Call NameLoad Call NameLoad Num BinOpAdd NameLoad Num body Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad NameLoad Expr Call NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad Num Expr Call AttributeLoad NameLoad attr NameLoad NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad Num decorator_list decorator_list ClassDef bases NameLoad body FunctionDef arguments args NameParam defaults body Assign NameStore AttributeLoad NameLoad attr Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad NameLoad Expr Call NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad BinOpSub NameLoad Num Expr Call AttributeLoad NameLoad attr Expr Call AttributeLoad NameLoad attr NameLoad NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad Num decorator_list decorator_list ClassDef bases NameLoad AttributeLoad NameLoad attr body FunctionDef arguments args NameParam defaults body Assign AttributeStore NameLoad attr Call NameLoad NameLoad NameLoad decorator_list FunctionDef arguments args NameParam defaults body Expr Call AttributeLoad NameLoad attr NameLoad NameLoad Call NameLoad NameLoad decorator_list decorator_list ClassDef bases NameLoad AttributeLoad NameLoad attr body FunctionDef arguments args NameParam defaults body Assign AttributeStore NameLoad attr Call NameLoad Call NameLoad NameLoad decorator_list decorator_list ClassDef bases NameLoad AttributeLoad NameLoad attr body FunctionDef arguments args NameParam defaults body Assign AttributeStore NameLoad attr Call NameLoad Call NameLoad NameLoad decorator_list decorator_list ClassDef bases NameLoad AttributeLoad NameLoad attr body FunctionDef arguments args NameParam defaults body Assign AttributeStore NameLoad attr Call NameLoad Call NameLoad Call NameLoad NameLoad decorator_list decorator_list ClassDef bases NameLoad AttributeLoad NameLoad attr body FunctionDef arguments args NameParam defaults body Assign NameStore Call NameLoad Call NameLoad NameLoad Assign AttributeStore NameLoad attr Call NameLoad NameLoad Assign AttributeStore NameLoad attr AttributeLoad NameLoad attr decorator_list decorator_list ClassDef bases NameLoad AttributeLoad NameLoad attr body FunctionDef arguments args NameParam defaults body Assign NameStore Call NameLoad Call NameLoad NameLoad Assign AttributeStore NameLoad attr Call NameLoad NameLoad Assign AttributeStore NameLoad attr AttributeLoad NameLoad attr decorator_list decorator_list ClassDef bases NameLoad AttributeLoad NameLoad attr body FunctionDef arguments args NameParam defaults body Assign NameStore Call AttributeLoad NameLoad attr Call NameLoad NameLoad Assign AttributeStore NameLoad attr Call NameLoad NameLoad Assign AttributeStore NameLoad attr AttributeLoad NameLoad attr decorator_list decorator_list ClassDef bases NameLoad AttributeLoad NameLoad attr body FunctionDef arguments args NameParam defaults body Assign NameStore Call AttributeLoad NameLoad attr Call NameLoad NameLoad Assign AttributeStore NameLoad attr Call NameLoad Call AttributeLoad NameLoad attr Assign AttributeStore NameLoad attr AttributeLoad NameLoad attr decorator_list decorator_list ClassDef bases NameLoad AttributeLoad NameLoad attr body FunctionDef arguments args NameParam defaults body Assign NameStore Call AttributeLoad NameLoad attr Call NameLoad NameLoad Assign AttributeStore NameLoad attr Call NameLoad Call AttributeLoad NameLoad attr Assign AttributeStore NameLoad attr AttributeLoad NameLoad attr decorator_list decorator_list ClassDef bases NameLoad AttributeLoad NameLoad attr body FunctionDef arguments args NameParam defaults body Assign NameStore Call NameLoad Call NameLoad NameLoad Assign AttributeStore NameLoad attr Call NameLoad NameLoad Assign AttributeStore NameLoad attr AttributeLoad NameLoad attr decorator_list decorator_list ClassDef bases NameLoad AttributeLoad NameLoad attr body FunctionDef arguments args NameParam defaults body Assign AttributeStore NameLoad attr Call NameLoad Call NameLoad NameLoad decorator_list FunctionDef arguments args NameParam defaults body Assign NameStore Call NameLoad Call NameLoad NameLoad Assign NameStore Call NameLoad NameLoad Expr Call NameLoad NameLoad Expr Call NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad BinOpSub NameLoad Num Expr Call AttributeLoad NameLoad attr NameLoad Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad BinOpSub NameLoad Num Assign SubscriptStore NameLoad Slice Num ListLoad Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad Num Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad ListLoad Expr Call AttributeLoad NameLoad attr Call NameLoad Num Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad Num decorator_list decorator_list ClassDef bases NameLoad AttributeLoad NameLoad attr body FunctionDef arguments args NameParam defaults body Assign AttributeStore NameLoad attr Call NameLoad Call NameLoad NameLoad decorator_list FunctionDef arguments args NameParam defaults body Assign NameStore Call NameLoad Call NameLoad NameLoad Assign NameStore Call NameLoad NameLoad Expr Call NameLoad NameLoad Expr Call NameLoad NameLoad Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad BinOpSub NameLoad Num Expr Call AttributeLoad NameLoad attr NameLoad Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad BinOpSub NameLoad Num Assign SubscriptStore NameLoad Slice Num ListLoad Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad Num Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad ListLoad Expr Call AttributeLoad NameLoad attr Call NameLoad Num Expr Call AttributeLoad NameLoad attr Call NameLoad NameLoad Num decorator_list decorator_list ClassDef bases NameLoad body FunctionDef arguments args NameParam defaults body Return Call NameLoad Call NameLoad Num decorator_list FunctionDef arguments args NameParam defaults body Raise Call NameLoad Str decorator_list decorator_list ClassDef bases NameLoad body FunctionDef arguments args NameParam defaults body Return Call NameLoad Call NameLoad Num decorator_list FunctionDef arguments args NameParam defaults body Raise Call NameLoad Str decorator_list decorator_list ClassDef bases NameLoad body FunctionDef arguments args NameParam defaults body Return Call NameLoad Call NameLoad Num decorator_list FunctionDef arguments args NameParam defaults body Return NameLoad decorator_list decorator_list ClassDef bases AttributeLoad NameLoad attr body FunctionDef arguments args NameParam defaults body Expr Call AttributeLoad NameLoad attr NameLoad NameLoad Call NameLoad Expr Call AttributeLoad NameLoad attr NameLoad NameLoad Call NameLoad Expr Call AttributeLoad NameLoad attr NameLoad AttributeLoad ListLoad attr Call NameLoad Expr Call AttributeLoad NameLoad attr NameLoad AttributeLoad ListLoad attr Call NameLoad Assign NameStore Call NameLoad Call NameLoad Num Expr Call AttributeLoad NameLoad attr NameLoad AttributeLoad NameLoad attr Call NameLoad Expr Call AttributeLoad NameLoad attr NameLoad AttributeLoad NameLoad attr Call NameLoad decorator_list FunctionDef arguments args NameParam defaults body Expr Call AttributeLoad NameLoad attr Call NameLoad Call NameLoad Call NameLoad Call NameLoad Num decorator_list decorator_list If CompareEq NameLoad Str body Expr Call AttributeLoad NameLoad attr
N/A N/A  Test Iterator Length Transparency

Some functions or methods which accept general iterable arguments have
optional, more efficient code paths if they know how many items to expect.
For instance, map(func, iterable), will pre-allocate the exact amount of
space required whenever the iterable can report its length.

The desired invariant is:  len(it)==len(list(it)).

A complication is that an iterable and iterator can be the same object. To
maintain the invariant, an iterator needs to dynamically update its length.
For instance, an iterable such as range(10) always reports its length as ten,
but it=iter(range(10)) starts at ten, and then goes to nine after next(it).
Having this capability means that map() can ignore the distinction between
map(func, iterable) and map(func, iter(iterable)).

When the iterable is immutable, the implementation can straight-forwardly
report the original length minus the cumulative number of calls to next().
This is the case for tuples, range objects, and itertools.repeat().

Some containers become temporarily immutable during iteration.  This includes
dicts, sets, and collections.deque.  Their implementation is equally simple
though they need to permanently set their length to zero whenever there is
an attempt to iterate after a length mutation.

The situation slightly more involved whenever an object allows length mutation
during iteration.  Lists and sequence iterators are dynamically updatable.
So, if a list is extended during iteration, the iterator will continue through
the new items.  If it shrinks to a point before the most recent iteration,
then no further items are available and the length is reported at zero.

Reversed objects can also be wrapped around mutable objects; however, any
appends after the current position are ignored.  Any other approach leads
to confusion and possibly returning the same item more than once.

The iterators not listed above, such as enumerate and the other itertools,
are not length transparent because they have no way to distinguish between
iterables that report static length and iterators whose length changes with
each call (i.e. the difference between enumerate('abc') and
enumerate(iter('abc')).

 N/A unittest test support itertools repeat collections deque builtins len _len N/A n 10 len N/A N/A obj N/A N/A N/A N/A N/A N/A _len obj N/A N/A N/A TypeError N/A N/A N/A N/A N/A N/A obj __length_hint__ N/A N/A N/A AttributeError N/A N/A TypeError N/A TestInvariantWithoutMutations N/A N/A test_invariant N/A N/A self N/A N/A N/A it N/A self it N/A i N/A reversed N/A range 1 N/A n 1 N/A N/A N/A N/A self assertEqual N/A len it i N/A N/A next it N/A N/A N/A self assertEqual N/A len it 0 N/A N/A N/A self assertRaises StopIteration next it N/A N/A N/A self assertEqual N/A len it 0 N/A N/A TestTemporarilyImmutable N/A TestInvariantWithoutMutations N/A test_immutable_during_iteration N/A N/A self N/A N/A N/A it N/A self it N/A N/A N/A self assertEqual N/A len it n N/A N/A next it N/A N/A N/A self assertEqual N/A len it N/A n 1 N/A N/A N/A self mutate N/A N/A N/A self assertRaises RuntimeError next it N/A N/A N/A self assertEqual N/A len it 0 N/A N/A TestRepeat N/A TestInvariantWithoutMutations N/A unittest TestCase N/A setUp N/A N/A self N/A N/A N/A N/A self it N/A repeat None n N/A test_no_len_for_infinite_repeat N/A N/A self N/A N/A N/A N/A N/A self assertRaises TypeError len N/A repeat None N/A N/A TestXrange N/A TestInvariantWithoutMutations N/A unittest TestCase N/A setUp N/A N/A self N/A N/A N/A N/A self it N/A iter N/A range n N/A N/A TestXrangeCustomReversed N/A TestInvariantWithoutMutations N/A unittest TestCase N/A setUp N/A N/A self N/A N/A N/A N/A self it N/A reversed N/A range n N/A N/A TestTuple N/A TestInvariantWithoutMutations N/A unittest TestCase N/A setUp N/A N/A self N/A N/A N/A N/A self it N/A iter N/A tuple N/A range n N/A N/A TestDeque N/A TestTemporarilyImmutable N/A unittest TestCase N/A setUp N/A N/A self N/A N/A N/A d N/A deque N/A range n N/A N/A self it N/A iter d N/A N/A self mutate N/A d pop N/A N/A TestDequeReversed N/A TestTemporarilyImmutable N/A unittest TestCase N/A setUp N/A N/A self N/A N/A N/A d N/A deque N/A range n N/A N/A self it N/A reversed d N/A N/A self mutate N/A d pop N/A N/A TestDictKeys N/A TestTemporarilyImmutable N/A unittest TestCase N/A setUp N/A N/A self N/A N/A N/A d N/A N/A dict fromkeys N/A range n N/A N/A self it N/A iter d N/A N/A self mutate N/A d popitem N/A N/A TestDictItems N/A TestTemporarilyImmutable N/A unittest TestCase N/A setUp N/A N/A self N/A N/A N/A d N/A N/A dict fromkeys N/A range n N/A N/A self it N/A iter N/A N/A d items N/A N/A self mutate N/A d popitem N/A N/A TestDictValues N/A TestTemporarilyImmutable N/A unittest TestCase N/A setUp N/A N/A self N/A N/A N/A d N/A N/A dict fromkeys N/A range n N/A N/A self it N/A iter N/A N/A d values N/A N/A self mutate N/A d popitem N/A N/A TestSet N/A TestTemporarilyImmutable N/A unittest TestCase N/A setUp N/A N/A self N/A N/A N/A d N/A set N/A range n N/A N/A self it N/A iter d N/A N/A self mutate N/A d pop N/A N/A TestList N/A TestInvariantWithoutMutations N/A unittest TestCase N/A setUp N/A N/A self N/A N/A N/A N/A self it N/A iter N/A range n N/A test_mutation N/A N/A self N/A N/A N/A d N/A list N/A range n N/A it N/A iter d N/A N/A next it N/A N/A next it N/A N/A N/A self assertEqual N/A len it N/A n 2 N/A N/A N/A d append n N/A N/A N/A self assertEqual N/A len it N/A n 1 N/A N/A d N/A 1 N/A N/A N/A N/A self assertEqual N/A len it 0 N/A N/A N/A self assertEqual N/A list it N/A N/A N/A N/A d extend N/A range 20 N/A N/A N/A self assertEqual N/A len it 0 N/A N/A TestListReversed N/A TestInvariantWithoutMutations N/A unittest TestCase N/A setUp N/A N/A self N/A N/A N/A N/A self it N/A reversed N/A range n N/A test_mutation N/A N/A self N/A N/A N/A d N/A list N/A range n N/A it N/A reversed d N/A N/A next it N/A N/A next it N/A N/A N/A self assertEqual N/A len it N/A n 2 N/A N/A N/A d append n N/A N/A N/A self assertEqual N/A len it N/A n 2 N/A N/A d N/A 1 N/A N/A N/A N/A self assertEqual N/A len it 0 N/A N/A N/A self assertEqual N/A list it N/A N/A N/A N/A d extend N/A range 20 N/A N/A N/A self assertEqual N/A len it 0 N/A N/A BadLen N/A object N/A __iter__ N/A N/A self N/A N/A N/A N/A iter N/A range 10 N/A __len__ N/A N/A self N/A N/A N/A N/A RuntimeError hello N/A N/A BadLengthHint N/A object N/A __iter__ N/A N/A self N/A N/A N/A N/A iter N/A range 10 N/A __length_hint__ N/A N/A self N/A N/A N/A N/A RuntimeError hello N/A N/A NoneLengthHint N/A object N/A __iter__ N/A N/A self N/A N/A N/A N/A iter N/A range 10 N/A __length_hint__ N/A N/A self N/A N/A N/A None N/A N/A TestLengthHintExceptions N/A N/A unittest TestCase N/A test_issue1242657 N/A N/A self N/A N/A N/A N/A N/A self assertRaises RuntimeError list N/A BadLen N/A N/A N/A self assertRaises RuntimeError list N/A BadLengthHint N/A N/A N/A self assertRaises RuntimeError N/A N/A extend N/A BadLen N/A N/A N/A self assertRaises RuntimeError N/A N/A extend N/A BadLengthHint N/A b N/A bytearray N/A range 10 N/A N/A N/A self assertRaises RuntimeError N/A b extend N/A BadLen N/A N/A N/A self assertRaises RuntimeError N/A b extend N/A BadLengthHint N/A test_invalid_hint N/A N/A self N/A N/A N/A N/A N/A self assertEqual N/A list N/A NoneLengthHint N/A list N/A range 10 N/A N/A N/A N/A __name__ __main__ N/A N/A N/A N/A unittest main