python constructor overloading

Michael W. Ryan mryan at netaxs.com
Fri Dec 17 16:22:14 EST 1999


On Fri, 17 Dec 1999 17:44:43 +0000, Greg Copeland
<gtcopeland at EarthLink.Net> wrote:

>Thanks for spelling this out.  I had, however, already read about this
>issue.  My objects will be explicitly collected (at least, my intent is
>to).  Err, rather, I assume that 'sys.exc_traceback = None' will do this
>after the contained have lived their life?  I poorly expressed my self
>here as I didn't believe it to be central to my point of contention. 
>Thanks.  I'll pay more attention next time!  :) 

If you're talking about circular references (which I think you are),
reference counts get messed up, and it has nothing to do with the
traceback.  David Beazly explained it well in "Python Essential Reference"
(pg. 19).  Given the following code:

  a = {}
  b = {}
  a['b'] = b  # a contains reference to b
  b['a'] = a  # b contains reference to a
  del a
  del b

  In this example, the del statements decrease the reference count of a
  and b and destroy the names used to refer to the underlying objects.
  However, because each object contains a reference to the other, the
  reference count doesn't drop to zero and the objects are not garbage
  collected.  As a result, the objects remain allocated even though
  there's no longer any way for the interpreter to access them (that is,
  the names used to refer to the objects are gone).

The best solution in this case, is to use your own cleanup method either
in the container or the contents and have them explicitely remove the
reference to the other.

>Hmmm.  I'll try again.  I guess it was something I was doing wrong.  I
>was getting an unbound method error.  As for the constructor/initializer
>reference.  To me, it looks like python's equivalent to a C++
>constructor, so that's what I called it.  Is "initializer" the correct
>terminology for python?

Yes, it's the correct term.  When you want to call the __init__ method of
a super class, you need to call it explicitely:
  superclass.__init__(self, args)

-- 
Michael W. Ryan, MCP, MCT     | OTAKON 2000
mryan at netaxs.com              | Convention of Otaku Generation
http://www.netaxs.com/~mryan/ | http://www.otakon.com/

PGP fingerprint: 7B E5 75 7F 24 EE 19 35  A5 DF C3 45 27 B5 DB DF
PGP public key available by fingering mryan at unix.netaxs.com (use -l opt)



More information about the Python-list mailing list