Online Book Reader

Home Category

Learning Python - Mark Lutz [181]

By Root 1718 0

spam ham eggs

Of course, if you can use 3.0 exclusively you can forget such mappings entirely, but many Python programmers will at least encounter, if not write, 2.X code and systems for some time to come.

* * *

Note


I use Python 3.0 print function calls throughout this book. I’ll usually warn you that the results may have extra enclosing parentheses in 2.6 because multiple items are a tuple, but I sometimes don’t, so please consider this note a blanket warning—if you see extra parentheses in your printed text in 2.6, either drop the parentheses in your print statements, recode your prints using the version-neutral scheme outlined here, or learn to love superfluous text.

* * *

* * *

Why You Will Care: print and stdout


The equivalence between the print operation and writing to sys.stdout is important. It makes it possible to reassign sys.stdout to any user-defined object that provides the same write method as files. Because the print statement just sends text to the sys.stdout.write method, you can capture printed text in your programs by assigning sys.stdout to an object whose write method processes the text in arbitrary ways.

For instance, you can send printed text to a GUI window, or tee it off to multiple destinations, by defining an object with a write method that does the required routing. You’ll see an example of this trick when we study classes in Part VI of this book, but abstractly, it looks like this:

class FileFaker:

def write(self, string):

# Do something with printed text in string

import sys

sys.stdout = FileFaker()

print(someObjects) # Sends to class write method

This works because print is what we will call in the next part of this book a polymorphic operation—it doesn’t care what sys.stdout is, only that it has a method (i.e., interface) called write. This redirection to objects is made even simpler with the file keyword argument in 3.0 and the >> extended form of print in 2.6, because we don’t need to reset sys.stdout explicitly—normal prints will still be routed to the stdout stream:

myobj = FileFaker() # 3.0: Redirect to object for one print

print(someObjects, file=myobj) # Does not reset sys.stdout

myobj = FileFaker() # 2.6: same effect

print >> myobj, someObjects # Does not reset sys.stdout

Python’s built-in input function reads from the sys.stdin file, so you can intercept read requests in a similar way, using classes that implement file-like read methods instead. See the input and while loop example in Chapter 10 for more background on this.

Notice that because printed text goes to the stdout stream, it’s the way to print HTML in CGI scripts used on the Web. It also enables you to redirect Python script input and output at the operating system’s shell command line, as usual:

python script.py < inputfile > outputfile

python script.py | filterProgram

Python’s print operation redirection tools are essentially pure-Python alternatives to these shell syntax forms.

* * *

* * *

[29] Technically, printing uses the equivalent of str in the internal implementation of Python, but the effect is the same. Besides this to-string conversion role, str is also the name of the string data type and can be used to decode Unicode strings from raw bytes with an extra encoding argument, as we’ll learn in Chapter 36; this latter role is an advanced usage that we can safely ignore here.

[30] In both 2.6 and 3.0 you may also be able to use the __stdout__ attribute in the sys module, which refers to the original value sys.stdout had at program startup time. You still need to restore sys.stdout to sys.__stdout__ to go back to this original stream value, though. See the sys module documentation for more details.

Chapter Summary

In this chapter, we began our in-depth look at Python statements by exploring assignments, expressions, and print operations. Although these are generally simple to use, they have some alternative forms that, while optional, are often convenient in practice: augmented assignment statements and the redirection form of print

Return Main Page Previous Page Next Page

®Online Book Reader