Routing sys.stdout.write (print)
dworkin at ccs.neu.edu.bbs
dworkin at ccs.neu.edu.bbs
Mon Jul 17 20:10:01 EDT 2000
Joseph Betz <betz at bmi.net> writes:
> I'm getting unexpected results from the following code:
>
> import sys
> from sys import stdout
> x = stdout
>
> class Writer
> def write(self, string):
> x.write(': ' + string)
> sys.stdout = Writer()
> print "Ni!"
> x.write('Ni!\n')
>
> The output I'm getting is:
>
> : Ni!:
> Ni!
>
> I am looking for a solution so that the second colon isn't in the
> output. A good solution
> may be very different then the code I have here.
This is due to the implementation of the print statement, which
effectively calls sys.stdout.write a separate time for the trailing
newline.
If you use sys.stdout.write() directly instead of the print statement,
you won't have this problem.
-Justin
More information about the Python-list
mailing list