title: Dynamic Python Class Definition in SQLAlchemy
body: <p>I'm creating a backend application with SQLAlchemy using the declarative base. The ORM requires about 15 tables each of which maps to a class object in SQLAlchemy. Because these class objects are all defined identically I thought a factory pattern could produce the classes more concisely. However, these classes not only have to be defined, they have to be assigned to unique variable names so they can be imported and used through the project.</p>
  
  <p>(Sorry if this question is a bit long, I updated it as I better understood the problem.)</p>
  
  <p>Because we have so many columns (~1000) we define their names and types in external text files to keep things readable. Having done that one way to go about declaring our models is like this:</p>
  
  <pre><code>class Foo1(Base):
      __tablename___ = 'foo1'
  
  class Foo2(Base):
      __tablename___ = 'foo2'
  
  ... etc
  </code></pre>
  
  <p>and then I can add the columns by looping over the contents of the external text file and using the <code>setattr()</code> on each class definition. </p>
  
  <p>This is OK but it feels too repetitive as we have about 15 tables. So instead I took a stab at writing a factory function that could define the classes dynamically.  </p>
  
  <pre><code>def orm_factory(class_name):
      class NewClass(Base):
          __tablename__ = class_name.lower()
      NewClass.__name__ = class_name.upper()
      return NewClass
  </code></pre>
  
  <p>Again I can just loop over the columns and use <code>setattr()</code>. When I put it together it looks like this:</p>
  
  <pre><code>for class_name in class_name_list:
      ORMClass = orm_factory(class_name)
      header_keyword_list = get_header_keyword_list(class_name)
      define_columns(ORMClass, header_keyword_list)
  </code></pre>
  
  <p>Where <code>get_header_keyword_list</code> gets the column information and <code>define_columns</code> performs the <code>setattr()</code> assignment. When I use this and run <code>Base.metadata.create_all()</code> the SQL schema get generated just fine.</p>
  
  <p>But, when I then try to import these class definitions into another model I get an error like this:</p>
  
  <pre><code>SAWarning: The classname 'NewClass' is already in the registry of this declarative base, mapped to &lt;class 'ql_database_interface.IR_FLT_0'&gt;
  </code></pre>
  
  <p>This, I now realize makes total sense based on what I learned yesterday: <a href="https://stackoverflow.com/questions/18326719/python-class-variable-name-vs-name">Python class variable name vs __name__</a>. </p>
  
  <p>You can address this by using <code>type</code> as a class generator in your factory function (as two of the answers below do). However, this does not solve the issue of being able to import the class because the while the classes are dynamically constructed in the factory function the variable the output of that function is assigned to is static. Even if it were dynamic, such as a dictionary key, it has to be in the module name space in order to be imported from another module. See my answer for more details.</p>