inherit without calling parent class constructor?

Daniel Dittmar daniel.dittmar at sap.corp
Wed Jan 26 13:03:45 EST 2005


Christian Dieterich wrote:
> I need to create many instances of a class D that inherits from a class 
> B. Since the constructor of B is expensive I'd like to execute it only 
> if it's really unavoidable. Below is an example and two workarounds, but 
> I feel they are not really good solutions. Does somebody have any ideas 
> how to inherit the data attributes and the methods of a class without 
> calling it's constructor over and over again?

- rename B to A
- class B (A)
- move the costly constructor from A to B
- class D (A)

You can now move some parts from B.__init__ to A.__init__ if they are 
really needed by D as well.

In OO speak: D is not really a subclass of B. Refactor the common code 
into a new class A.

Daniel



More information about the Python-list mailing list