Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes perfect description, decorator <code>donothing</code> decorates the function <code>printer</code> and returns a object of class <code>donothing</code>, so yes decorator simply boils down to this </p> <pre><code>x = donothing(func) # donothing is a class not function </code></pre> <p>and you can use it like this, if you wish to avoid @deco syntax.</p> <p>so now x is an object, when you do x(), <code>__call__</code> of that object is called and there it calls the function which was passed in <code>__init__</code></p> <p><strong>edit:</strong> Second decorator is wrong , because it only checks the parameter but never calls the function being passed and function being passed to decorator is names <code>params</code> but should be name something like func or better name</p> <p>you can test that it does nothing by passing correct params</p> <pre><code>params = { 'name': 'John Doe', 'pass': 'OpenSesame', 'code': '1134', } complex_function(params=params) </code></pre> <p>it doesn't print the arguments as complex_function is supposed to do.</p> <p>so correct decorator is </p> <pre><code>class checkforkeysinparams(object): def __init__(self, required): self.required = set(required) def __call__(self, func): def wrapper(params): missing = self.required.difference(params) if missing: raise TypeError('Missing from "params" argument: %s' % ', '.join(sorted(missing))) func(params) return wrapper </code></pre> <p>In first example Class is being used as decorator itself, here the object of class <code>checkforkeysinparams</code> is used as deocrator hence function gets passed to <code>__call__</code> of that object</p>
    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