title: Python decorators
body: <p>I have a question about decorators. I understand what are decorators and I know how to use it, I have read all this tutorial <a href="https://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python?lq=1">How to make a chain of function decorators?</a></p>
  
  <p>I understand that :</p>
  
  <pre><code>&gt;&gt;&gt; def my_decorator(fn):
  &gt;&gt;&gt;     print 'Do something before'
  &gt;&gt;&gt;     print fn()
  
  
  &gt;&gt;&gt; def foo():
  &gt;&gt;&gt;     return 'Hello World!'
  
  &gt;&gt;&gt; foo = my_decorator(foo)
  </code></pre>
  
  <p>Is the same at that :</p>
  
  <pre><code>&gt;&gt;&gt; def my_decorator(fn):
  &gt;&gt;&gt;     print 'Do something before'
  &gt;&gt;&gt;     print fn()
  
  &gt;&gt;&gt; @my_decorator
  &gt;&gt;&gt; def foo():
  &gt;&gt;&gt;     return 'Hello World!'
  </code></pre>
  
  <p>I know what are closures and why we use closure in a decorator with parameters (to get the decorator parameters in nested function) but that I don't understand is why we use closure and nested functions to get arguments and the function.</p>
  
  <p>How the closure (or something else) can access parameters and the function outside. I am unable to do the same without the @decorator.</p>
  
  <p>Here for example I can access my foo() and the parameters of the function without passing this function in parameter :</p>
  
  <pre><code>def my_decorator(str):
      def wrapper(fn):
          def inner_function(*args):
              print 'Do something before'
              return fn(*args)
          return inner_function
      return wrapper
  
  @my_decorator('test')
  def foo(a, b):
      return a + b
  
  
  print foo(1, 1)
  </code></pre>
  
  <p>How this is possible ?</p>