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. Advertising

  2. 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/
    Jeremy Sanders, Oct 17, 2007
    #2
    1. Advertising

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

    MRAB Guest

    On Oct 17, 4:01 pm, Jeremy Sanders <jeremy
    > wrote:
    > 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
    MRAB, Oct 17, 2007
    #4
  5. sophie_newbie

    Paul Hankin Guest

    On Oct 17, 3:57 pm, 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?


    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
    Paul Hankin, Oct 18, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ra294

    Now() Fucntion and CurrentCulture

    ra294, Nov 25, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    617
    Hans Kesting
    Nov 26, 2004
  2. TOMERDR
    Replies:
    6
    Views:
    371
    Jonathan Mcdougall
    May 22, 2006
  3. thunk
    Replies:
    1
    Views:
    308
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    477
    thunk
    Apr 1, 2010
  5. thunk
    Replies:
    14
    Views:
    617
    thunk
    Apr 3, 2010
Loading...

Share This Page