One of my biggest pet peeve in #python is the open() API. It should not have a "text mode", and it should certainly not be the default.

All files are binary, there is not a "text world" and a "bytes worlds".

Some binary files represent encoded text.

I hate it because it gives people the wrong idea on how things work. I think dart got it right, it separates opening the file and parsing the text inside::

file.openRead().transform(utf8.decoder).transform(new LineSplitter())

I wish we did this for Python. open() would have only read/write/append modes, not "text" or "bytes" non sense, and always produce bytes. But the returned file like object should have a text() method so that you can do:

with open(file).text('utf8') as f:
for l in f:

@bitecode I admire your impulse here, but this would be a usability disaster unless "text open" were the default and "bytes open" were something else.

The reason is that you very rarely actually manipulate files as bytes - you mainly use some sort of abstraction. Almost certainly "put some text in this file" is the most common abstraction people use. "Why do I need to always call the `.text` method every time I open a file?" would be one of the top complaints about the ergonomics of Python.

Not to mention people would probably get confused and start using bytestrings.

Follow

@bitecode Despite the fact that it makes the abstraction of "open a file" less clean, I think it was definitely the right choice.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.