[Python-Dev] Replacement for print in Python 3.0

Simon Percivall s.percivall at chello.se
Mon Sep 5 20:04:07 CEST 2005

On 5 sep 2005, at 18.56, Stephan Deibel wrote:
> On Mon, 5 Sep 2005, Martin Blais wrote:
>> However, there is an easy way out: hijack sys.stdout to forward to
>> your logger system.
>> I've got a web application framework that's setup like that right  
>> now,
>> it works great (if you will not need the original print-to-stdout
>> anymore in your program, that is).  I print, it goes to the logfile.
>> You just have to be careful where--in time-- you replace sys.stdout.
> Sure, and indeed I've done that often enough but it's kind of ugly and
> doesn't help if you merge bodies of code where some stuff should go to
> a log, some to stdout, some elsewhere.
> Hmm, maybe I'd end up avoiding the builtin print() as well, or at
> least need to pass around the stream where I want output.  The general
> problem of not tying code to a particular output stream is what I'm
> reacting to.

Easy, just always print to a file-like object when you think you  
might have
to switch destination later, and control the output from there:

class Out:
     def write(self, text):
         # switch to logging here, or something

out = Out()

print >>out, "I won't have to change this statement at all!"

Print being a statement or a function doesn't matter in this case.  
replacing is a bitch either way.


More information about the Python-Dev mailing list