10

According to Tim Peters, "There should be one-- and preferably only one --obvious way to do it." In Python, there appears to be three ways to print information:

print('Hello World', end='')
sys.stdout.write('Hello World')
os.write(1, b'Hello World')

Question: Are there best-practice policies that state when each of these three different methods of printing should be used in a program?

Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
  • Do you want to use it for logging? Then I would suggest the logging module: https://docs.python.org/3/library/logging.html – RvdK Jul 02 '14 at 13:53
  • I do think `print` is the only obvious way (say you don't know python and you come from perl, what might you try?). This can be said for magic methods as well. – C.B. Jul 02 '14 at 13:54
  • @NoctisSkytower correct, you should also list 'print "some text"'. Note the missing brackets. – RvdK Jul 02 '14 at 14:07
  • 2
    @RvdK There is an effort to switch to print() vs. print. print is just a relic of older python that we are stuck with in python 2.x – Paul Seeb Jul 02 '14 at 14:12

3 Answers3

9

Note that the statement of Tim is perfectly correct: there is only one obvious way to do it: print(). The other two possibilities that you mention have different goals.

If we want to summarize the goals of the three alternatives:

  • print is the high-level function that allow you to write something to stdout(or an other file). It provides a simple and readable API, with some fancy options about how the single items are separated, or whether you want to add or not a terminator etc. This is what you want to do most of the time.

  • sys.stdout.write is just a method of the file objects. So the real point of sys.stdout is that you can pass it around as if it were any other file. This is useful when you have to deal with a function that is expecting a file and you want it to print the text directly on stdout.

    In other words you shouldn't use sys.stdout.write at all. You just pass around sys.stdout to code that expects a file.

    Note: in python2 there were some situations where using the print statement produced worse code than calling sys.stdout.write. However the print function allows you to define the separator and terminator and thus avoids almost all these corner cases.

  • os.write is a low-level call to write to a file. You must manually encode the contents and you also have to pass the file descriptor explicitly. This is meant to handle only low level code that, for some reason, cannot be implemented on top of the higher-level interfaces. You almost never want to call this directly, because it's not required and has a worse API than the rest.

Note that if you have code that should write down things on a file, it's better to do:

my_file.write(a)
# ...
my_file.write(b)
# ...
my_file.write(c)

Than:

print(a, file=my_file)
# ...
print(b, file=my_file)
# ...
print(c, file=my_file)

Because it's more DRY. Using print you have to repeat file= everytime. This is fine if you have to write only in one place of the code, but if you have 5/6 different writes is much easier to simply call the write method directly.

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
2

To me print is the right way to print to stdout, but :

There is a good reason why sys.stdout.write exists - Imagine a class which generates some text output, and you want to make it write to either stdout, and file on disk, or a string. Ideally the class really shouldn't care what output type it is writing to. The class can simple be given a file object, and so long as that object supports the write method, the class can use the write method to output the text.

Tony Suffolk 66
  • 9,358
  • 3
  • 30
  • 33
2

Two of these methods require importing entire modules. Based on this alone, print() is the best standard use option.

sys.stdout is useful whenever stdout may change. This gives quite a bit of power for stream handling.

os.write is useful for os specific writing tasks (non blocking writes for instance)

This question has been asked a number of times on this site for sys.stdout vs. print:

Python - The difference between sys.stdout.write and print

print() vs sys.stdout.write(): which and why?

One example for using os.write (non blocking file writes demonstrated in the question below). The function may only be useful on some os's but it still must remain portable even when certain os's don't support different/special behaviors.

How to write to a file using non blocking IO?

Community
  • 1
  • 1
Paul Seeb
  • 6,006
  • 3
  • 26
  • 38