title: Redirecting stdout to "nothing" in python
body: <p>I have a large project consisting of sufficiently large number of modules, each printing something to the standard output. Now as the project has grown in size, there are large no. of <code>print</code> statements printing a lot on the std out which has made the program considerably slower.</p>
  
  <p>So, I now want to decide at <em>runtime</em> whether or not to print anything to the stdout. I cannot make changes in the modules as there are plenty of them. (I know I can redirect the stdout to a file but even this is considerably slow.)  </p>
  
  <p>So my question is how do I redirect the stdout to nothing ie how do I make the <code>print</code> statement do nothing?</p>
  
  <pre><code># I want to do something like this.
  sys.stdout = None         # this obviously will give an error as Nonetype object does not have any write method.
  </code></pre>
  
  <p>Currently the only idea I have is to make a class which has a write method (which does nothing) and redirect the stdout to an instance of this class.</p>
  
  <pre><code>class DontPrint(object):
      def write(*args): pass
  
  dp = DontPrint()
  sys.stdout = dp
  </code></pre>
  
  <p>Is there an inbuilt mechanism in python for this? Or is there something better than this?</p>