Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between sys.stdout.write and print?

Are there situations in which sys.stdout.write() is preferable to print?

(Examples: better performance; code that makes more sense)

like image 956
Johanna Larsson Avatar asked Jul 16 '10 09:07

Johanna Larsson


People also ask

What is SYS stdout write?

A built-in file object that is analogous to the interpreter's standard output stream in Python. stdout is used to display output directly to the screen console. Output can be of any form, it can be output from a print statement, an expression statement, and even a prompt direct for input.

Is print the same as stdout?

The print function will also put a space before the object if it is not the start of a line and a newline character at the end. When you use stdout, that time you need to convert the object to a string by yourself and you will do it by calling "str", and there is no newline character.

Is print the same as stdout in python?

In Python, whenever we use print() the text is written to Python's sys. stdout, whenever input() is used, it comes from sys. stdin, and whenever exceptions occur it is written to sys. stderr.

What is the difference between write and print in Python?

print outputs a text representation of an object, whereas write outputs raw bytes. Try x=7310302560386184563 and look at println(x) versus write(stdout, x); println() to see the difference.


2 Answers

print is just a thin wrapper that formats the inputs (modifiable, but by default with a space between args and newline at the end) and calls the write function of a given object. By default this object is sys.stdout, but you can pass a file using the "chevron" form. For example:

print >> open('file.txt', 'w'), 'Hello', 'World', 2+3 

See: https://docs.python.org/2/reference/simple_stmts.html?highlight=print#the-print-statement


In Python 3.x, print becomes a function, but it is still possible to pass something other than sys.stdout thanks to the fileargument.

print('Hello', 'World', 2+3, file=open('file.txt', 'w')) 

See https://docs.python.org/3/library/functions.html#print


In Python 2.6+, print is still a statement, but it can be used as a function with

from __future__ import print_function 

Update: Bakuriu commented to point out that there is a small difference between the print function and the print statement (and more generally between a function and a statement).

In case of an error when evaluating arguments:

print "something", 1/0, "other" #prints only something because 1/0 raise an Exception  print("something", 1/0, "other") #doesn't print anything. The function is not called 
like image 56
luc Avatar answered Sep 23 '22 11:09

luc


print first converts the object to a string (if it is not already a string). It will also put a space before the object if it is not the start of a line and a newline character at the end.

When using stdout, you need to convert the object to a string yourself (by calling "str", for example) and there is no newline character.

So

print 99 

is equivalent to:

import sys sys.stdout.write(str(99) + '\n') 
like image 27
dogbane Avatar answered Sep 26 '22 11:09

dogbane