Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:45:51+00:00 2026-06-05T12:45:51+00:00

I have a rather complex base class for some sqlalchemy models and I want

  • 0

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 new 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:

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')

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:45:52+00:00Added an answer on June 5, 2026 at 12:45 pm

    This happens because your lambda function references column but doesn’t pass it in as an argument:

    lambda cls, v: cls.setAttribute( cls, column, v )
    

    When this function is executed, it will look for the name column in a containing or global scope, and always find the value 'username' because that is what column was set to last.

    Here is a straightforward way to fix this using a default argument value:

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

    Another alternative would be to use a closure (in a way, the mutable default argument is a type of closure):

        def __new__( cls, *args, **kwargs ):
            def make_setter(column):
                return lambda cls, v: cls.setAttribute( cls, column, v )
            for column in cls.columns:
                setattr( cls, "set%s" % column.capitalize(), make_setter(column))
            return super( Test, cls ).__new__( cls, *args, **kwargs )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rather complex mapping for some of my class objects for NHibernate.
I have a PHP class that stores a complex multidimensional array, and rather than
I have a legacy class that is rather complex to maintain: class OldClass {
I have a large old program which has some rather complex graphical displays (all
I have a set of rather complex ORM modules that inherit from Class::DBI .
I have a rather complex decorator written by someone else. What I want to
Have a (rather complex) app that works fine on iOS 4 but fails on
I'm new to programming but have managed to build a rather complex dynamic site
I have a rather complex data source for my UITableView. I am very carefully
We have a rather large and complex application written in Java which is running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.