title: Adding attributes to instance methods in Python
body: <p>I would like to add an attribute to an instance method in one of my classes.  I tried the answer given in <a href="https://stackoverflow.com/questions/279561/what-is-the-python-equivalent-of-static-variables-inside-a-function">this question</a>, but this answer only works for functions -- as far as I can tell.</p>
  
  <p>As an example, I would like to be able to do something like:</p>
  
  <pre><code>class foo(object):
      ...
      def bar(self):
          self.bar.counter += 1
          return self.bar.counter
      bar.counter = 1
      ...
  </code></pre>
  
  <p>but, when I call foo().bar() I get:</p>
  
  <pre><code>AttributeError: 'instancemethod' object has no attribute 'counter'
  </code></pre>
  
  <p>My goal in doing this is to try to impress that the 'counter' variable is local to the bar() method, and also to avoid cluttering my class namespace with yet another attribute.  Is there a way to do this?  Is there a more pythonic way of doing this?</p>