inherit without calling parent class constructor?

From: Christian Dieterich (cdieterich_at_geosci.uchicago.edu)
Date: 01/26/05


Date: Wed, 26 Jan 2005 11:29:48 -0600
To: python-list@python.org

Hi,

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?

Thank,

Christian

Here's the "proper" example:

class B:
     def __init__(self, length):
         size = self.method(length)
         self.size = size
     def __str__(self):
         return 'object size = ' + str(self.size)
     def method(self, length):
         print 'some expensive calculation'
         return length

class D(B):
     def __init__(self, length):
         B.__init__(self, length)
         self.value = 1

if __name__ == "__main__":
     obj = D(7)
     obj = D(7)

Here's a workaround:

class B:
     def __init__(self, length):
         size = self.method(length)
         self.size = size
     def __str__(self):
         return 'object size = ' + str(self.size)
     def method(self, length):
         print 'some expensive calculation'
         return length

class D(B):
     def __init__(self, object):
         for key, value in object.__dict__.iteritems():
             setattr(self, key, value)
         self.value = 1

if __name__ == "__main__":
     tmp = B(7)
     obj = D(tmp)
     obj = D(tmp)

Here's another workaround:

Bsize = 0
class B:
     def __init__(self, length):
         size = self.method(length)
         self.size = size
         global Bsize
         Bsize = self.size
     def __str__(self):
         return 'object size = ' + str(self.size)
     def method(self, length):
         print 'some expensive calculation'
         return length

class D(B):
     def __init__(self, length):
         self.size = Bsize
         self.value = 1

if __name__ == "__main__":
     B(7)
     obj = D(9)
     obj = D(9)



Relevant Pages

  • Re: Useful classical inheritance example?
    ... have implicit reference to Object.prototype. ... What are you mean about "parent"? ... constructor: In class-based inheritance, ... constructed inherits through the prototype chain. ...
    (comp.lang.javascript)
  • Re: Need help: about OOP inheritance/abstract class
    ... but you can create a class that instantiates the user control in the ... >> constructor, or that inherits it, and inherit the interfaces. ...
    (microsoft.public.dotnet.general)
  • Re: Question about inheritance and constructors.
    ... Now, let's say that I have class B that inherits from A. B automagically gets all of the properties and methods of A, but I cannot leverage any of A's contructors without redefining them on B and delegating. ... Public Class DownloadClient ... Public Sub New(ByVal UserName As String) ... inherit the constructor of 'DownloadClient', ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Difference Between Calling SuperClass Constructor or Inherited Set Method
    ... I have the usual base class w/ private instance ... Derived inherits directly from Object ... the first form of the constructor. ... A Starbuck's shop has "public methods" that anyone can call ...
    (comp.lang.java.programmer)
  • Re: Class diagrams for javascript
    ... BTW, as `Utils' cannot be used as a constructor reference, it should be `utils'. ... prototype chain, not from a class. ... And the other object again inherits from ... yet another next object in its prototype chain, ...
    (comp.lang.javascript)