468,549 Members | 2,199 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,549 developers. It's quick & easy.

Stopping a fucntion from printing its output on screen

Hi, in my program i need to call a couple of functions that do some
stuff but they always print their output on screen. But I don't want
them to print anything on the screen. Is there any way I can disable
it from doing this, like redirect the output to somewhere else? But
later on in the program i then need to print other stuff so i'd need
to re-enable printing too. Any ideas?

Oct 17 '07 #1
4 25770
sophie_newbie wrote:
Hi, in my program i need to call a couple of functions that do some
stuff but they always print their output on screen. But I don't want
them to print anything on the screen. Is there any way I can disable
it from doing this, like redirect the output to somewhere else? But
later on in the program i then need to print other stuff so i'd need
to re-enable printing too. Any ideas?
If they are python functions, this hack should work...

import sys

class NullWriter(object):
def write(self, arg):
pass

def testfunc():
print "this is a test"

nullwrite = NullWriter()
oldstdout = sys.stdout
sys.stdout = nullwrite # disable output
testfunc()
sys.stdout = oldstdout # enable output
testfunc()
--
Jeremy Sanders
http://www.jeremysanders.net/
Oct 17 '07 #2
On Wed, 17 Oct 2007 07:57:04 -0700, sophie_newbie wrote:
Hi, in my program i need to call a couple of functions that do some
stuff but they always print their output on screen. But I don't want
them to print anything on the screen. Is there any way I can disable it
from doing this, like redirect the output to somewhere else? But later
on in the program i then need to print other stuff so i'd need to
re-enable printing too. Any ideas?

If it is your program, then just change your program to not print to the
screen! Instead of writing a function like this:
def parrot():
# This is bad practice!
do_lots_of_calculations()
print "This is a parrot"
write it like this:

def parrot():
# This is good practice
do_lots_of_calculations()
return "This is a parrot"
What's the difference? In the first version, the function parrot()
decides that its result is always printed. In the second version, YOU
decide:
result = parrot()
# now pass the result to something else
do_more_calculations(result)
# or print it
print result
Otherwise, you can do something like this:

import cStringIO
import sys
capture_output = cStringIO.StringIO()
sys.stdout = capture_output
# call the function that always prints
parrot()
# now restore stdout
sys.stdout = sys.__stdout__
but that's a little risky, and I recommend against it unless you have no
other choice.
--
Steven
Oct 17 '07 #3
On Oct 17, 4:01 pm, Jeremy Sanders <jeremy
+complangpyt...@jeremysanders.netwrote:
sophie_newbie wrote:
Hi, in my program i need to call a couple of functions that do some
stuff but they always print their output on screen. But I don't want
them to print anything on the screen. Is there any way I can disable
it from doing this, like redirect the output to somewhere else? But
later on in the program i then need to print other stuff so i'd need
to re-enable printing too. Any ideas?

If they are python functions, this hack should work...

import sys

class NullWriter(object):
def write(self, arg):
pass

def testfunc():
print "this is a test"

nullwrite = NullWriter()
oldstdout = sys.stdout
sys.stdout = nullwrite # disable output
testfunc()
sys.stdout = oldstdout # enable output
testfunc()
You might want to guarantee that the output is re-enabled even if
testfunc() raises an exception:

nullwrite = NullWriter()
oldstdout = sys.stdout
sys.stdout = nullwrite # disable output
try:
testfunc()
finally:
sys.stdout = oldstdout # enable output

Oct 17 '07 #4
On Oct 17, 3:57 pm, sophie_newbie <paulgeele...@gmail.comwrote:
Hi, in my program i need to call a couple of functions that do some
stuff but they always print their output on screen. But I don't want
them to print anything on the screen. Is there any way I can disable
it from doing this, like redirect the output to somewhere else? But
later on in the program i then need to print other stuff so i'd need
to re-enable printing too. Any ideas?
Yes, in your functions that you may or may not want to print stuff,
declare them with a stream parameter that defaults to stdout.

For example:

import sys

def f(i, out = sys.stdout)
# Do something...
print >>out, "i is %d" % i

Then usually, you call
f(10)

But when you want to elide the output, use Jeremy's nullwriter:
class NullWriter(object):
def write(self, arg):
pass
nullwriter = NullWriter()

f(10, out = nullwriter)

Having the output stream explicit like this is much better style than
abusing sys.stdout, and it won't go wrong when errors occur. It's the
same idea as avoiding global variables.

--
Paul Hankin

Oct 18 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Tinus | last post: by
7 posts views Thread by DazedAndConfused | last post: by
1 post views Thread by mehdi | last post: by
10 posts views Thread by sophie_newbie | last post: by
7 posts views Thread by Iain Wilson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.