title: How to create a decorator that can be used either with or without parameters?
body: <p>I'd like to create a Python decorator that can be used either with parameters:</p>
  
  <pre><code>@redirect_output("somewhere.log")
  def foo():
      ....
  </code></pre>
  
  <p>or without them (for instance to redirect the output to stderr by default):</p>
  
  <pre><code>@redirect_output
  def foo():
      ....
  </code></pre>
  
  <p>Is that at all possible?</p>
  
  <p>Note that I'm not looking for a different solution to the problem of redirecting output, it's just an example of the syntax I'd like to achieve.</p>