Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you use a decorator, you're replacing one function with another. In other words, if you have a decorator</p> <pre><code>def logged(func): def with_logging(*args, **kwargs): print func.__name__ + " was called" return func(*args, **kwargs) return with_logging </code></pre> <p>then when you say</p> <pre><code>@logged def f(x): """does some math""" return x + x * x </code></pre> <p>it's exactly the same as saying</p> <pre><code>def f(x): """does some math""" return x + x * x f = logged(f) </code></pre> <p>and your function <code>f</code> is replaced with the function with_logging. Unfortunately, this means that if you then say</p> <pre><code>print f.__name__ </code></pre> <p>it will print <code>with_logging</code> because that's the name of your new function. In fact, if you look at the docstring for <code>f</code>, it will be blank because <code>with_logging</code> has no docstring, and so the docstring you wrote won't be there anymore. Also, if you look at the pydoc result for that function, it won't be listed as taking one argument <code>x</code>; instead it'll be listed as taking <code>*args</code> and <code>**kwargs</code> because that's what with_logging takes.</p> <p>If using a decorator always meant losing this information about a function, it would be a serious problem. That's why we have <code>functools.wraps</code>. This takes a function used in a decorator and adds the functionality of copying over the function name, docstring, arguments list, etc. And since <code>wraps</code> is itself a decorator, the following code does the correct thing:</p> <pre><code>from functools import wraps def logged(func): @wraps(func) def with_logging(*args, **kwargs): print func.__name__ + " was called" return func(*args, **kwargs) return with_logging @logged def f(x): """does some math""" return x + x * x print f.__name__ # prints 'f' print f.__doc__ # prints 'does some math' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload