title: Python Dynamic Subclassing
body: <p>Possible duplicates:
  <a href="https://stackoverflow.com/questions/9269902/is-there-a-way-to-create-subclasses-on-the-fly">Is there a way to create subclasses on-the-fly?</a>
  <a href="https://stackoverflow.com/questions/3915024/dynamically-creating-classes-python">Dynamically creating classes - Python</a></p>
  
  <p>I would like to create a subclass where the only difference is some <em>class variable</em>, but I would like everything else to stay the same. I need to do this dynamically, because I only know the class variable value at run time. </p>
  
  <p>Here is some example code so far. I would like FooBase.foo_var to be "default" but FooBar.foo_var to be "bar." No attempt so far has been successful.</p>
  
  <pre><code>class FooBase(object):
      foo_var = "default"
  
      def __init__(self, name="anon"):
          self.name = name
  
      def speak(self):
          print self.name, "reporting for duty"
          print "my foovar is '" +  FooBase.foo_var + "'"
  
  
  if __name__ == "__main__":
      #FooBase.foo_var = "foo"
      f = FooBase()
      f.speak()
  
      foobarname = "bar"
      #FooBar = type("FooBar", (FooBase,), {'foo_var': "bar"})
      class FooBar(FooBase): pass
      FooBar.foo_var = "bar"
      fb = FooBar()
      fb.speak()
  </code></pre>
  
  <p>Many thanks</p>
  
  <p><b>EDIT</b> so I obviously have a problem with this line:</p>
  
  <p><code>print "my foovar is '" +  FooBase.foo_var + "'"</code></p>
  
  <p>The accepted answer has <code>self.foo_var</code> in the code. That's what I should be doing. I feel ridiculous now.</p>