python decorators and static methods
<p>I want to add decorator with my python static method, like following:</p>

<pre><code>class AdminPanelModel(db.Model):

    id = db.Column('id', db.Integer, primary_key=True)
    visible = db.Column(db.Boolean,)


    def decor_init(func):
        def func_wrapper(*kargs, **kwargs):
            for l in model.all(): #internal logic
                pass

            return func(*kargs, **kwargs)

        return func_wrapper

    @staticmethod
    @decor_init
    def all_newscollection_at_adminpanel():
        pass
</code></pre>

<p>I tried adding <code>@staticmethod</code> to my decorator and tried to make function call like following <code>func(AdminPanelModel,*kargs, **kwargs)</code>, but no luck, still stuck at following error message:</p>

<pre><code>TypeError: unbound method func_wrapper() must be called with AdminPanelModel instance as first argument (got nothing instead)
</code></pre>

<p>Is there any way I can achieve that? I am aware of that there can be other way I can do the same work, but its more of an educational question rather than get things done somehow.</p>

<p>You need to use a <code>classmethod</code> instead:</p>

<pre><code>@classmethod
@decor_init
def all_newscollection_at_adminpanel(cls):
    pass
</code></pre>

<p>The call is the same, but <code>classmethod</code>s implicitly receive the class as the first argument, which will then also be passed into the decorated function</p>

<p>to see the difference:</p>

<pre><code>class AdminPanelModel(object):
    def decor_init(func):
        def func_wrapper(*kargs, **kwargs):
            print kargs    # The first element should be of type class AdminPanelModel
            return func(*kargs, **kwargs)

        return func_wrapper

    @staticmethod
    @decor_init
    def staticm():
        pass

    @classmethod
    @decor_init
    def classm(cls):
        pass
</code></pre>

<p>they yield</p>

<pre><code>AdminPanelModel.staticm()
&gt;&gt;&gt; ()                                      # empty, which is reason for error
AdminPanelModel.classm()
&gt;&gt;&gt; (&lt;class '__main__.AdminPanelModel'&gt;,)   # class instance as first parameter
</code></pre>

python sqlalchemy