Python 2.0

Salvador Fandiño fandino at
Thu Jun 3 14:16:51 EDT 1999

Graham Matthews wrote:

> You don't need a new method call -- all you need is a mark and sweep
> collector with refcounts. I really am amazed that this has generated so
> much traffic. It's very simple. You have ref counts just like you do
> now in Python. When an objects refcount goes to 0 its __del__ method
> is called. On top of this you add a mark and sweep collector to handle
> circular references. During a sweep if it finds dead objects it calls
> the __del__ method for that object.

GC usually deletes objects in random order which is not a very good
behavior. With a new magic method like __gc__ you could determine
adequate entry points to ciclid data structures deletion.

Suppose you have an object DB that encapsulates a database and a
persistent object O that automatically stores itself in DB when changed:

class DB_class:
	def __del__(s): s.db_close()
	def store(s,o):

class O_class:
	def store(s,DB):
	def __del__(s):
		if s.changed_p:
So if you delete DB before O (that could happen if the GC behavior was
to call __del__), O will never become stored.

Adding the new method __gc__ to O_class and changing the GC behavior to
just call __gc__ solves the problem:

	def __gc__(s):

- Salva.

More information about the Python-list mailing list