On Wed, May 13, 2020 at 10:51:58AM -0700, Andrew Barnert via Python-ideas wrote:
Students often want to know why this doesn’t work:
[snip example showing the difference between iterators which become exhausted after iteration, and sequences that don't]
The answer is that files are iterators, while lists are… well, there is no word.
Sequences. Containers. Non-iterator-iterables. There's three words :-)
Albeit one of them is hyphenated, but if we were German we might call it a noniteratoriterablesequenceobject and abbreviate it to NIISO :-)
Oh, there's also subscriptable, to describe things that can be subscripted. Or there's "list-like".
The glossary defines all of Iterator, Iterable, Sequence but not Container:
There's no short, common word specifically for iterables that aren't iterators for the same reason that there's no short, common word specifically for dogs that aren't poodles, plants that aren't roses, or programming languages that aren't Python :-)
A file object is a file, in the same way that a list object is a list and an int object is an int.
A file object is a proxy to a file. It doesn't itself live in a file system, but it points to an entity which does. In the same way that it is both useful and necessary to distinguish between an iterable that obeys the iterator protocol and one that does not, it's useful and necessary to distinguish between an in-memory file object and the file it points to.
File objects have a close method; files don't. And as you point out, file objects are iterators, but files are just a bunch of bytes on a hard drive.