Meaning of the "@package" decorator in Python
<p>I'm trying to understand a class method definition which reads similar to the following:</p>

<pre><code>@package(type='accounts', source='system')
def get(self, [other arguments]):
    [function body]
</code></pre>

<p>What is the meaning of the <code>@package</code> decorator? I was unable to find documentation on this.</p>

<p>There is no default <code>package</code> decorator in the Python standard library.</p>

<p>A decorator is <em>just a simple expression</em>; there will be a <code>package()</code> callable in the same module (either defined there as a function or class, or imported from another module).</p>

<p>The line <code>@package(type='accounts', source='system')</code> executes the expression <code>package(type='accounts', source='system')</code>, and the return value of that is used to decorate the <code>get()</code> function. You could read it as:</p>

<pre><code>def get(self, [other arguments]):
    [function body]
get = package(type='accounts', source='system')(get)
</code></pre>

<p>except the name <code>get</code> is set just once.</p>

<p>For example, <code>package</code> could be defined as:</p>

<pre><code>def package(type='foo', source='bar'):
    def decorator(func):
        def wrapper(*args, **kwargs):
            # do something with type and source
            return func(*args, **kwargs)
        return wrapper
    return decorator
</code></pre>

<p>so <code>package()</code> returns <code>decorator()</code>, which in turn returns <code>wrapper()</code>; <code>package()</code> is a <em>decorator factory</em>, producing the actual decorator.</p>

python