0

I am using a python module that generates a lot of stdout/stderr Future warnings when calling its constructor like this:

foo_obj = foo(par1, par2)

I can't really edit, or suppress the output inside the module, since I have no access to it/don't want to mess with it.

Is there a way to easily do something like:

if verbose:
    foo_obj = foo(par1, par2)
    #generating lots of output

else:
    foo_obj = foo(par1, par2)
    #and do something fancy to the above to not generate and stdout/stderr 

Edit/Clarification

If I take the solution from Suppress stdout / stderr print from Python functions I cannot suppress the output. I have constructed a more elaborate example here:

import numpy as np
import warnings

class WritingOutput(object):
    def __init__(self, b):
        self._a = 5
        self._b = b 
        self.generate_output()

    def generate_output(self):
        print("This is normal output")
        warnings.warn("This is a warning")
        print ('a is %d and b is %s ' %(self._a, self._b))

wo_obj = WritingOutput('10')
print (wo_obj._a)
with suppress_stdout_stderr():
    wo_obj = WritingOutput('10')

With the suggested fix the output is still printed. I must admit that I don't quite understand how the fix should work in the first place. I need something that would return both the output that is written out (both stderr and stdout) in WritingOutput, as well as the object I generate. I can't change the function call in the constructor of my actual problem.

Community
  • 1
  • 1
Toni
  • 1
  • 1
  • Possible duplicate of [Suppress stdout / stderr print from Python functions](http://stackoverflow.com/questions/11130156/suppress-stdout-stderr-print-from-python-functions) – Jeff B Jan 04 '16 at 15:57
  • No, at least I don't see how I can make the solution of that thread work with my problem. – Toni Jan 05 '16 at 16:48

0 Answers0