title: Monkeypatch with instance method
body: <p>I'm trying to monkeypatch how <code>pandas</code> Panel's slicing (<code>__getitem__</code>).  This is straightforward to do with a basic function, foo.</p>
  
  <pre><code>from pandas import Panel
  Panel.__getitem__ = ORIGINAL_getitem
  
  
  def newgetitem(panel, *args, **kwargs):
      """ Append a string to return of panel.__getitem__"""
      out = super(Panel, panel).__getitem__(*args, **kwargs)
      return out+'custom stuff added'
  
  Panel.__getitem__ = newgetitem
  </code></pre>
  
  <p>Where<code>ORIGINAL_getitem</code> is storing the original Panel method.  I'm trying to extend to the case where <code>foo()</code> is not a function, but an instance method of an object, <code>Foo</code>.  For example:</p>
  
  <pre><code>class Foo:
  
      name = 'some name'
  
      def newgetitem(self, panel, *args, **kwargs):
          """ Append a string to return of panel.__getitem__,
          but take attributes from self, like self.name
          """
          out = super(Panel, panel).__getitem__(*args, **kwargs)
          return out+'custom stuff added including name' + self.name
  </code></pre>
  
  <p><code>Foo.foo()</code> must access the attribute <code>self.name</code>.  Therefore, the monkeypatched function would need a reference to the Foo instance somehow, in addition to the Panel.  How can I monkepatch panel with <code>Foo.foo()</code> and make self.name accessible?  </p>
  
  <p>The switching between the monkey patched function happens in another method, <code>Foo.set_backend()</code></p>
  
  <pre><code>class Foo:
  
      name = 'some name'
  
      def foo(self):
          return 'bar, called by %s' % self.name
  
      def set_backend(self, backend):
          """ Swap between new or original slicing."""
          if backend != 'pandas':
              Panel.__getitem__ = newgetitem            
          else:
              Panel.__getitem__ = ORIGINAL_getitem
  </code></pre>
  
  <p>What I really need is for <code>newgetitem</code> to maintain a reference to <code>self</code>.  </p>
  
  <h2>Solution Attempts</h2>
  
  <p>So far I've tried taking making <code>newgetitem()</code> a pure function, and using partial functions to pass a reference to self in.  This doesn't work.  Something like:</p>
  
  <pre><code>import functools
  
  def newgetitem(foo_instance, panel, *args, **kwargs):
      ....
  
  class Foo:
  
      ...
      def set_backend(self, backend):
          """ Swap between new or original slicing."""
          if backend != 'pandas':
              partialfcn = functools.partial(newgetitem, self)
              Panel.__getitem__ = partialfcn            
          else:
              Panel.__getitem__ = ORIGINAL_getitem
  </code></pre>
  
  <p>But this doesn't work.  A reference to self is passed, but no access from the calling Panel possible. That is:</p>
  
  <pre><code> panel['50']  
  </code></pre>
  
  <p>Passes a reference to <code>Foo</code>, not to <code>Panel</code>.</p>
  
  <p><strong>Yes, I know this is bad practice, but it's just a workaround for the time-being.</strong></p>