title: Python: wrapping method invocations with pre and post methods
body: <p>I am instantiating a class A (which I am importing from somebody
  else, so I can't modify it) into my class X.</p>
  
  <p>Is there a way I can intercept or wrap calls to methods in A?
  I.e., in the code below can I call</p>
  
  <pre><code>x.a.p1()
  </code></pre>
  
  <p>and get the output</p>
  
  <pre><code>X.pre
  A.p1
  X.post
  </code></pre>
  
  <p>Many TIA!</p>
  
  <pre><code>class A:
      # in my real application, this is an imported class
      # that I cannot modify
      def p1(self): print 'A.p1'
  
  class X:
      def __init__(self):
          self.a=A()
      def pre(self): print 'X.pre'
      def post(self): print 'X.post'
  
  x=X()
  x.a.p1()
  </code></pre>