python constructor overloading

Darrell news at dorb.com
Thu Dec 16 21:13:21 EST 1999


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.

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.

Here's some example code.

Code from Jesse Sweeney:
Subject: Re: How can I make my assertions smarter?

import sys

def upglobals():
    try:
        1/0
    except ZeroDivisionError:
        return sys.exc_info()[2].tb_frame.f_back.f_back.f_globals

def uplocals():
    try:
        1/0
    except ZeroDivisionError:
        return sys.exc_info()[2].tb_frame.f_back.f_back.f_locals

def pre(condition, locals=None, globals=None):
    if not locals:
        locals = uplocals()
    if not globals:
        globals = upglobals()
    if not eval(condition, locals, globals):
        raise "Precondition Failure", condition



--
--Darrell
"Greg Copeland" <gtcopeland at EarthLink.Net> wrote in message
news:3859904B.631359C8 at EarthLink.Net...
> Okay, I have two classed in a container object.  I'd like to be able to
> pass the container to both of the contained objects so that they can
> call some methods that exist in the container.  Both objects are derived
> from objects in another library.  So, I don't want to have to change the
> other objects (as that would be anti-OO and anti-reuse, IMOHO).  At any
> rate, my first thought was that I would overload the constructor of my
> newly derived objects.  The problem is, I'm not sure how to do this.  I
> looke in the FAQ, needless to say, those solutions suck.  As it stands,
> it doesn't really look like you can overload constructors.  The end
> result that I'm looking for is something like this:
>
> # This is from another library
> class base
>
> # This is mine
> class derived( base ):
> def __init__( self, caller, arg1, arg2, arg3 ):
> self.caller = caller
> base.__init__( arg1, arg2, arg3 )
>
> As you can see, I still want the base class' constructor, as I'm really
> attempting to extend the functionality in the derived class.  Of course,
> it would be dandy if there is something that will give me the caller's
> reference!  I realize that there are probably other ways to do this, but
> this seems like the right direction (more C++'ish - maybe that's the
> problem).
>
> Please excuse the ignorance of the python newbie!
>
> Thanks in advanced,
> Greg
>
> 
>
> --
> http://www.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list