I would like to add an attribute to an instance method in one of my classes. I tried the answer given in this question, but this answer only works for functions -- as far as I can tell.

As an example, I would like to be able to do something like:

class foo(object):
    ...
    def bar(self):
        self.bar.counter += 1
        return self.bar.counter
    bar.counter = 1
    ...

but, when I call foo().bar() I get:

AttributeError: 'instancemethod' object has no attribute 'counter'

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?

Edit
Report