ownership problem?

Alex Martelli aleax at mail.comcast.net
Tue Nov 22 04:57:25 CET 2005


Jeffrey Schwab <jeff at schwabcenter.com> wrote:
   ...
> > You may be gratified to learn that Python's main storage model
> > is reference counted objects, and when an object falls out of
> > all referenced scopes its finalizers run immediately.
> 
> Thanks, that's good to know!  For some reason I had it in my head that
> Python always used mark & sweep.  I'm used to ref-counted collection in
> Perl, but I never relied on it because of a nagging (probably 
> ill-founded) worry about cyclic references.  Does Python have any way
> around this?  Is there a Python equivalent to C++ destructors?

Python (main implementation, known as CPython) uses mark-and-sweep (with
generations &c) to deal with cyclic garbage -- but destructors (__del__
methods) *interfere* with cyclic garbage collection (there may be no
safe order of freeing a bunch of cyclic garbage when objects have
__del__ methods, and Python can't find it if there is, anyway).

> I think Java has had a big influence, too.  People just don't seem to
> want to be bothered with thinking about object life cycles at all.  This
> seems unfortunate to me, because cleanup still has to be done, so it 
> just ends up getting moved outside the objects where it belongs.  I 
> think this hurts abstraction.

Python 2.5 should introduce a 'with' statement that may go partways
towards meeting your qualms; it's an approved PEP, though I do not
recall its number offhand.


Alex



More information about the Python-list mailing list