Old Man Yells At Cloud

Steve D'Aprano steve+python at pearwood.info
Tue Sep 19 20:23:15 EDT 2017


On Wed, 20 Sep 2017 04:31 am, bartc wrote:

> On 19/09/2017 17:30, Steve D'Aprano wrote:

[snip list of problems with print]

> Can't you get around all those with things like sys.stdout.write?

If you had kept reading, you would have seen that I wrote:

    Of course an experienced Python coder can work around all these 
    problems. The main way to work around them is to *stop using print*.

Using sys.stdout.write has disadvantages:

- it only takes a single string argument;

- which makes you responsible for doing everything that print does:

  * converting arbitrary arguments to strings;
  * joining them with spaces (or some other separator);
  * appending a newline (or other delimiter) at the end;
  * flushing the buffer;

- it's about three times as long to type as "print";

- are you sure sys.stdout is pointing where you expect?

So to work around *those* problems, you end up writing your own helper function,
which invariably ends up being called something like print1.


> If so, what was the point of having a discrete print statement/function
> at all?

It is easily discoverable for beginners.

You don't have to learn the concepts of modules, importing, attribute access,
methods, files, string escapes \n etc before being able to display output to
the user.

Don't get me wrong. I love print. Even the print statement is better than
nothing. But once you get past the simple cases:

    print "Hello World!"

you quickly run into its limitations.


-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list