title: Unable to understand this python decorator
body: <p>I am new to python decorators. I have understood the basic concepts with the help of simple examples. But when I tried to read this more practical decorator, I feel lost. Given below is the code followed by my questions:</p>
  
  <pre><code>class countcalls(object):
     "Decorator that keeps track of the number of times a function is called."
  
     __instances = {}
  
     def __init__(self, f):
        self.__f = f
        self.__numcalls = 0
        countcalls.__instances[f] = self
  
     def __call__(self, *args, **kwargs):
        self.__numcalls += 1
        return self.__f(*args, **kwargs)
  
     def count(self):
        "Return the number of times the function f was called."
        return countcalls.__instances[self.__f].__numcalls
  
  @countcalls
  def f():
     print 'f called'
  
  f()
  f()
  f()
  print f.count() # prints 3
  </code></pre>
  
  <p>My doubts:</p>
  
  <ol>
  <li><p>When we prefix the decorator to a function, does that mean that we are creating an object of the decorator class right there? In our case, when it says:</p>
  
  <p><code>@countcalls</code>
     <code>def f():
         print 'f called'</code></p></li>
  </ol>
  
  <p>Is <code>@countcalls</code> equivalent to creating a <code>countcalls</code> object and passing the function below to its <code>__init__</code> method?</p>
  
  <ol>
  <li><p>The <code>__call__</code> is taking three arguments. <code>self</code> is fine as far as the question above is answered. What the hell are the two other arguments: <code>*args, **kwargs</code> and what are they achieving?</p></li>
  <li><p>How can I get better at decorators?</p></li>
  </ol>