On Mon, Jan 25, 2021 at 4:42 PM Steven D'Aprano <steve@pearwood.info> wrote:
On Sat, Jan 23, 2021 at 09:11:27PM +1100, Chris Angelico wrote:

> > On the other hand, if we add `open_text()`:
> >
> > * Replacing open with open_text is easier than adding `, encoding="utf-8"`.
> > * Teachers can teach to use `open_text` to open text files. Students
> > can use "utf-8" by default without knowing about what encoding is.
> >
> > So `open_text()` can provide better developer experience, without
> > waiting 10 years.
>
> But this has a far worse end goal - two open functions with subtly
> incompatible defaults, and a big question of "why should I choose this
> over that".

It has an easy answer:

- Are you opening a text file and you don't know about or want to deal
  with encodings? Use `open_text`.

- Otherwise, use `open`.

I think that if we moved to an open_text() builtin, it should have the
simplest possible signature:

    open_text(filename, mode='r')

If you care about anything beyond that, use `open`.


> And if you start using open_text, suddenly your code won't
> work on older Pythons.

"Using older Pythons" is mostly a concern for library maintainers, not
beginners. A few years from now, Python 3.10 will be the oldest version
the great majority of beginners will care about, and 3.9 will be as
irrelevant to them as 3.4 is to us today.

Library maintainers always have to deal with the issue of not being able
to use the newest functionality, it doesn't prevent us from adding new
functionality.

Older Pythons may be easy to drop, but I'm not so sure about older unofficial docs. The open() function is very popular and there must be millions of blog posts with examples using it, most of them reading text files (written by bloggers naive in Python but good at SEO).

I would be very sad if the official recommendation had to become "[for the most common case] avoid open(filename), use open_text(filename)".

BTW remind me what open_text() would do? How would it differ from open() with the same arguments? That's too many messages back.
 
--
--Guido van Rossum (python.org/~guido)