Python has several io base classes including
IOBase
RawIOBase
BufferedIOBase
TextIOBase
as well as several derived io classes:
FileIO
BytesIO
Now, when i create a BytesIO
object, the mro is:
[<class '_io.BytesIO'>, <class '_io._BufferedIOBase'>, <class '_io._IOBase'>, <class 'object'>]
and when i create a FileIO
object, the mro is:
[<class '_io.FileIO'>, <class '_io._RawIOBase'>, <class '_io._IOBase'>, <class 'object'>]
This is pretty straightforward.
However, when i open a binary file for writing using the built-in open, i get the mro:
[<class '_io.BufferedWriter'>, <class '_io._BufferedIOBase'>, <class '_io._IOBase'>, <class 'object'>]
Isn't a command which opens a file expected to return a FileIO
object according to the principle of least surprise?
I just wrote a method which accepts either a BytesIO
or a file and i stumbled over my if isinstance(io.FileIO) ...
clause.
What is the difference between a FileIO
object and the object returned by open?