How to add static fields to a class via decorator?
<p>I have a problem with sqlalchemy and legacy database. Suppose I have a models like that</p>

<pre><code>class A(Base):
    foo = Column(String)
    modified_at = Column(DateTime, default=func.now, onupdate=func.now())

class B(Base):
    bar = Column(String)
    modified_at = Column('SOME_NAME', DateTime, default=func.now, onupdate=func.now())
</code></pre>

<p>As you can see there is a code duplication here in modified_at field. Both classes have modified_at field to track time of last modification, but with different column names.</p>

<p>This could be solved using metaclasses, but it would actually require basically the same amount of code to pass column name as it is with solution with code duplication (especially in python2 as in python2 you can't pass arguments to metaclass easily)</p>

<p>So what I thought of is to use decorate class</p>

<pre><code>def modified_at(col_name=None):
    def decorator(cls):
        cls.modified_at = Column(col_name, Datetime) # etc
        return cls
return decorator

@modified_at('SOME_NAME')
class B(Base):
    bar = Column(String)
</code></pre>

<p>But this gives me error when instantiating B(bar='some value')</p>

<pre><code>TypeError decorator() got an unexpected keyword argument 'bar'
</code></pre>