Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three ways to print in Python -- when to use each?

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?

like image 632
Noctis Skytower Avatar asked Jul 02 '14 13:07

Noctis Skytower


People also ask

What is the use of print () method?

print(): print() method in Java is used to display a text on the console. This text is passed as the parameter to this method in the form of String. This method prints the text on the console and the cursor remains at the end of the text at the console.


2 Answers

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.

like image 52
Bakuriu Avatar answered Sep 28 '22 08:09

Bakuriu


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.

like image 20
Tony Suffolk 66 Avatar answered Sep 28 '22 08:09

Tony Suffolk 66