Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

A file-handle is an iterator

  • collections
  • Iterator
  • Iterable
  • io
  • TextIOWrapper
  • issubclass

This slightly a repetition of the previous statement, that filehandles are iterators.

from collections.abc import Iterator, Iterable
from io import TextIOWrapper

with open(__file__) as fh:
    print(fh.__class__.__name__)
    print(issubclass(fh.__class__, TextIOWrapper))
    print(issubclass(fh.__class__, Iterator))
    print(issubclass(fh.__class__, Iterable))

    for line in fh:
        pass
        #print(line, end="")

Output:

TextIOWrapper
True
True
True