How to define and instantiate a derived class at once in python?
<p>I have a base class that I want to derive and instantiate together. I can do that in java like:  </p>

<pre><code>BaseClass derivedClassInstance = new BaseClass() {
    @override
    void someBaseClassMethod() { // my statements}
};
</code></pre>

<p>In python I can derive and and instantiate a base class like:  </p>

<pre><code>class DerivedClass(BaseClass):
    def some_base_class_method():
        # my statements

derived_class_instance = DerivedClass()
</code></pre>

<p>I need to sub-class single instances of some objects with minor changes. Deriving and assigning them separately seems like overkill.  </p>

<p>Is there a Java-like <em>one-liner</em> way to derive and instantiate a class on the fly? Or is there a more concise way to do what I did in python?</p>

<p>I think you are looking for metaclass programming. </p>

<pre><code>class Base(object):
   def test(self):
      print 'hit'
a =type( 'Derived',(Base,),{})()
a.test()
hit
</code></pre>

python inheritance