title: Python: How do you intercept a method call to change function parameters?
body: <p>What I am trying to do is write a wrapper around another module so that I can transform the parameters that are being passed to the methods of the other module. That was fairly confusing, so here is an example:</p>
  
  <pre><code>import somemodule
  
  class Wrapper:
      def __init__(self):
          self.transforms = {}
          self.transforms["t"] = "test"
  
      # This next function is the one I want to exist
      # Please understand the lines below will not compile and are not real code
      def __intercept__(self, item, *args, **kwargs):
          if "t" in args:
              args[args.index("t")] = self.transforms["t"]
          return somemodule.item(*args, **kwargs)
  </code></pre>
  
  <p>The goal is to allow users of the wrapper class to make simplified calls to the underlying module without having to rewrite all of the functions in the module. So in this case if <code>somemodule</code> had a function called <code>print_uppercase</code> then the user could do</p>
  
  <pre><code>w = Wrapper()
  w.print_uppercase("t")
  </code></pre>
  
  <p>and get the output</p>
  
  <pre><code>TEST
  </code></pre>
  
  <p>I believe the answer lies in <code>__getattr__</code> but I'm not totally sure how to use it for this application.</p>