Can subclasses in Python inherit parent class decorator
<p>If I apply a decorator to a class:</p>

<pre><code>from flask.ext.restful import Resource

@my_decorator()
class Foo(Resource): ...
</code></pre>

<p>Will it be automatically applied to any subclass methods as well? For example, will it be <em>magically</em> applied to <code>a_foobar_method</code>?</p>

<pre><code>class FooBar(Foo):

    def a_foobar_method(self):
        ...
</code></pre>

<p>In short, no.</p>

<pre><code>@my_decorator
class Foo(Resource): ...
</code></pre>

<p>is simply shorthand for</p>

<pre><code>class Foo(Resource): ...
Foo = my_decorator(Foo)
</code></pre>

<p>If the only time you ever use <code>Foo</code> is to define <code>FooBar</code>, then together your two pieces of code are equivalent to this:</p>

<pre><code>class Foo(Resource): ...
Foo_decorated = my_decorator(Foo)
class FooBar(Foo_decorated):
    def a_foobar_method(self):
        ...
</code></pre>

<p>Or even to this:</p>

<pre><code>class Foo(Resource): ...
class FooBar(my_decorator(Foo)):
    def a_foobar_method(self):
        ...
</code></pre>

<p>The real answer depends on what the decorator does. In principle it stands a chance of seeing the methods of <code>FooBar</code> if it looks at the list of methods after the class has been instantiated as an object (e.g. because it modifies the <code>__init__</code> method). But if it goes through the methods at the time it is called and applies some change to each of them, then it will have no effect on extra methods (or replaced methods) in <code>FooBar</code>. </p>

python python-decorators