Stopping a fucntion from printing its output on screen

Discussion in 'Python' started by sophie_newbie, Oct 17, 2007.

  1. 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?
     
    sophie_newbie, Oct 17, 2007
    #1
    1. Advertisements

  2. 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, Oct 17, 2007
    #2
    1. Advertisements


  3. 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 D'Aprano, Oct 17, 2007
    #3
  4. sophie_newbie

    MRAB Guest

    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
     
    MRAB, Oct 17, 2007
    #4
  5. sophie_newbie

    Paul Hankin Guest

    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, 2007
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.