File-like objects
There is a notion in Python of "file-like objects". This isn't as formal as a specific protocol, but, thanks to the polymorphism afforded by duck-typing, it works well in practice.
The reason it's not closely specified is that different types of data streams and devices have many different capabilities, expectations, and behaviors. So in fact defining a set of protocols to model them would be quite complex, and it wouldn't actually gain us much in practice, other than a smug sense of theoretical achievement. This is where the EAFP philosophy comes into its own: If you want to perform seek() on a file-like object without knowing in advance that it supports random access, go ahead and try (literally!). Just be prepared to ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access