python constructor overloading

Greg Copeland gtcopeland at EarthLink.Net
Fri Dec 17 10:54:00 EST 1999


Darrell wrote:
> 
> Keeping a reference to the caller can setup a circular reference.
> Which means memory leak. You might pass id(caller) and have
> a place to map from id ==> caller.

In this particular case, I wouldn't expect this to happen.  Since the
container should technically live longer than the contained.  In short,
if the container is gone, everything within it should also be gone.  No
memory leaked.

> 
> It sounds like the container will be constructing these instances. Then
> there are ways of using an exception to walk the stack and get variables
> from previous frames. Same as pdb does. I not sure about the righteousness
> of this.
> Such as I wonder if such code will work in future versions of Python.

Ouch!  Horrors or horrors.  I can't believe this.  I'd rather implement
a non-OO concept (have floating global functions and vars) than to have
to implement trickery, smoke, and least not, mirrors.

> 
> Here's some example code.
> 
> Code from Jesse Sweeney:
> Subject: Re: How can I make my assertions smarter?

[code and Greg's ramblings removed]

Elsewhere, in another thread, I saw someone talking about has-a and
is-a.  These, IMOHO, are fundamental OO-concepts.  Let's say that I have
a car.  The car has an ("has-a") engine,  ("has-a") transmission, and a
("has-a") guage-panel.  Let's say that the engine needs to pass vaccum
data to the transmission.  Furthermore, let's say that the transmission
needs to pass rpm data to the gauge-panel.  To me, the obvious solution
would be to do this:

class car:
	__init__( self ):
		g = guage(self)
		t = transmission( self )
		e = engine( self )

Now:
def engine.tranny_event( x )
	car.t.set_vaccume( x )

Whereby, g, t and e are keeping a reference to car.  I think you get the
idea.  This works well (in theory) as long as I'm creating the engine
and transmission classes.  What if I want to use someone else's engine,
but add a turbo changer to it?  Now, assume the original engine class
doesn't allow the container reference.

# Yes, I know that a turbo is not a ("is-a") engine.  For the sake of
example, we'll assume that if it directly bolts to it, it's part of the
engine object.  

class turboEngine( engine ):
	def __init__( self, container, boost, stuff ):
		self.container = container
		self.turbo( boost )
		self.engine.__init__( stuff )          # Do do you do this???

	def turbo( boost ):
		self.hp = log( boost )                 # snicker

Obviously, I want to keep all of the existing engine's functionality,
but simply extend it with the turbo's boost methods.  As I understand
it, you can't do this?!?!?  If I understand you correctly, I have to
re-implement engine's constructor and extend it in the turboEngine's
constructor.  If that's the case, I greatly condom the OO-implementation
of python.  So it isn't so.  If this is that case, I'd say the python
authors have been using MFC way too much!  :P~

To continue my original rants, if you can't do this, the next obvious
solution is to make the gauge a global and remove it entirely from the
has-a concept (i.e. it is no longer contained - rather, "it-is").  This,
in one shot, defeats one of the fundamental OOD philosophies.

Help me out,
	Greg




More information about the Python-list mailing list