Why can't I restore my redirected stdout?

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Wed Jun 7 17:44:59 EDT 2000


Raymond Ng Tong Leng wrote in comp.lang.python:
> I am trying to implement a verbose and quiet mode in my Python script. So I
> wrote the following class:
> 
> class Quiet:
>     def write(self, data):
>         pass
> 
> Then I set sys.stdout to Quiet by:
> 
> >>> sys.stdout = Quiet()
> >>> print 'hello'
> >>>
> 
> 'hello' doesn't come out which is what I want. But, when I tried to restore
> sys.stdout's original settings:
> 
> >>> sys.stdout = sys.__stdout__
> >>> print 'hello'
> >>>
> 
> 'hello' is not printed either! How do you restore sys.stdout to its original
> settings?

I just typed this in, and it worked fine! Something else must be happening.
Are you in idle or so? Does it work inside a normal python interpreter?
What's your platform?

> Also, I was wondering, are there other ways to suppress
> sys.stdout? I'm just curious whether I could suppress sys.stdout without
> redirecting to a class. It seems to me that I should be able to redirect
> sys.stdout to a null device or something. Thanks!

Seems to me your solution is the easiest way to get a cross platform null
device, but maybe there's something like it in the library already, I don't
know.

-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
Hi! I'm a .sig virus! Join the fun and copy me into yours!



More information about the Python-list mailing list