python create class methods on the fly
<p>I have a rather complex base class for some sqlalchemy models and I want to create a rails style setters but, since i'm fairly new to python, I'm stuck with a problem that I cant seem to bypass. I create the setters on the <strong>new</strong> method so I can have the triggered both on new instances and on queries but no matter what setter I define and execute it always picks the last setter to execute. An example serves better:</p>

<pre><code>class Test(object):

    columns = ['email', 'username']

    def __new__( cls, *args, **kwargs ):
        for column in cls.columns:
            setattr( cls, "set%s" % column.capitalize(), lambda cls, v: cls.setAttribute( cls, column, v ) )
        return super( Test, cls ).__new__( cls, *args, **kwargs )

    @staticmethod
    def setAttribute(cls, attribute, value):
        print "Setting attribute %s with value %s" % ( attribute, value )
        setattr( cls, attribute, value )

test = Test()
test.setEmail('test@test.com')
</code></pre>

<p>As you can see I'm setting the email but, when executed, the code tries to set the username which is the last column. Any idea why is that?</p>
