and for readers keeping countwe have just run this single more pager script in four different waysby importing and calling its functionby passing filename commandline argumentby redirecting stdin to fileand by piping command' output to stdin by supporting importable functionscommand-line argumentsand standard streamspython system tools code can be reused in wide variety of modes redirecting streams to python objects all of the previous standard stream redirections work for programs written in any language that hook into the standard streams and rely more on the shell' command-line processor than on python itself command-line redirection syntax like filename and program is evaluated by the shellnot by python more pythonesque form of redirection can be done within scripts themselves by resetting sys stdin and sys stdout to file-like objects the main trick behind this mode is that anything that looks like file in terms of methods will work as standard stream in python the object' interface (sometimes called its protocol)and not the object' specific datatypeis all that matters that isany object that provides file-like read methods can be assigned to sys stdin to make input come from that object' read methods any object that defines file-like write methods can be assigned to sys stdoutall standard output will be sent to that object' methods because print and input simply call the write and readline methods of whatever objects sys stdout and sys stdin happen to referencewe can use this technique to both provide and intercept standard stream text with objects implemented as classes if you've already studied pythonyou probably know that such plug-and-play compatibility is usually called polymorphism--it doesn' matter what an object isand it doesn' matter what its interface doesas long as it provides the expected interface this liberal approach to datatypes accounts for much of the conciseness and flexibility of python code hereit provides way for scripts to reset their own streams example - shows utility module that demonstrates this concept example - pp \system\streams\redirect py ""file-like objects that save standard output text in string and provide standard input text from stringredirect runs passed-in function with its output and input streams reset to these file-like class objects""import sys get built-in modules class outputsimulated output file standard streams