title: How to wrap built-in methods in Python? (or 'how to pass them by reference')
body: <p>I want to wrap the default open method with a wrapper that should also catch exceptions. Here's a test example that <strong>works</strong>:</p>
  
  <pre><code>truemethod = open
  def fn(*args, **kwargs):
      try:
          return truemethod(*args, **kwargs)
      except (IOError, OSError):
          sys.exit('Can\'t open \'{0}\'. Error #{1[0]}: {1[1]}'.format(args[0], sys.exc_info()[1].args))
  
  open = fn
  </code></pre>
  
  <p>I want to make a generic method of it:</p>
  
  <pre><code>def wrap(method, exceptions = (OSError, IOError)):
      truemethod = method
      def fn(*args, **kwargs):
          try:
              return truemethod(*args, **kwargs)
          except exceptions:
              sys.exit('Can\'t open \'{0}\'. Error #{1[0]}: {1[1]}'.format(args[0], sys.exc_info()[1].args))
  
      method = fn
  </code></pre>
  
  <p>But it doesn't work:</p>
  
  <pre><code>&gt;&gt;&gt; wrap(open)
  &gt;&gt;&gt; open
  &lt;built-in function open&gt;
  </code></pre>
  
  <p>Apparently, <code>method</code> is a copy of the parameter, not a reference as I expected. Any pythonic workaround?</p>