Learning Python - Mark Lutz [176]
Because the standard output stream is available in Python as the stdout file object in the built-in sys module (i.e., sys.stdout), it’s possible to emulate print with file write method calls. However, print is noticeably easier to use and makes it easy to print text to other files and streams.
Printing is also one of the most visible places where Python 3.0 and 2.6 have diverged. In fact, this divergence is usually the first reason that most 2.X code won’t run unchanged under 3.X. Specifically, the way you code print operations depends on which version of Python you use:
In Python 3.X, printing is a built-in function, with keyword arguments for special modes.
In Python 2.X, printing is a statement with specific syntax all its own.
Because this book covers both 3.0 and 2.6, we will look at each form in turn here. If you are fortunate enough to be able to work with code written for just one version of Python, feel free to pick the section that is relevant to you; however, as your circumstances may change, it probably won’t hurt to be familiar with both cases.
The Python 3.0 print Function
Strictly speaking, printing is not a separate statement form in 3.0. Instead, it is simply an instance of the expression statement we studied in the preceding section.
The print built-in function is normally called on a line of its own, because it doesn’t return any value we care about (technically, it returns None). Because it is a normal function, though, printing in 3.0 uses standard function-call syntax, rather than a special statement form. Because it provides special operation modes with keyword arguments, this form is both more general and supports future enhancements better.
By comparison, Python 2.6 print statements have somewhat ad-hoc syntax to support extensions such as end-of-line suppression and target files. Further, the 2.6 statement does not support separator specification at all; in 2.6, you wind up building strings ahead of time more often than you do in 3.0.
Call format
Syntactically, calls to the 3.0 print function have the following form:
print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout])
In this formal notation, items in square brackets are optional and may be omitted in a given call, and values after = give argument defaults. In English, this built-in function prints the textual representation of one or more objects separated by the string sep and followed by the string end to the stream file.
The sep, end, and file parts, if present, must be given as keyword arguments—that is, you must use a special “name=value” syntax to pass the arguments by name instead of position. Keyword arguments are covered in depth in Chapter 18, but they’re straightforward to use. The keyword arguments sent to this call may appear in any left-to-right order following the objects to be printed, and they control the print operation:
sep is a string inserted between each object’s text, which defaults to a single space if not passed; passing an empty string suppresses separators altogether.
end is a string added at the end of the printed text, which defaults to a \n newline character if not passed. Passing an empty string avoids dropping down to the next output line at the end of the printed text—the next print will keep adding to the end of the current output line.
file specifies the file, standard stream, or other file-like object to which the text will be sent; it defaults to the sys.stdout standard output stream if not passed. Any object with a file-like write(string) method may be passed, but real files should be already opened for output.
The textual representation of each object to be printed is obtained by passing the object to the str built-in