Algorithm for finalizing cycles (Re: [Python-Dev] Garbage collecting closures)

Guido van Rossum guido@python.org
Tue, 15 Apr 2003 15:06:15 -0400


> > > If the object having a finalizer doesn't support references to
> > > arbitrary other objects, then the application cannot make this
> > > object be part of a cycle.

> Greg Ewing <greg@cosc.canterbury.ac.nz> writes:
> 
> > It could make a subclass, though...

> From: martin@v.loewis.de (Martin v. =?iso-8859-15?q?L=F6wis?=)
> 
> If the type is carefully designed, it can't...

I suppose you have something in mind like this (which is the only way
I can come up with to implement something like a 'final' class in pure
Python):

  >>> class C(object): 
  ...   def __new__(cls):
  ...     if cls is not C: raise TypeError, "haha"
  ...     return object.__new__(cls)
  ...
  >>> class D(C): pass
  ...    
  >>> a = D()
  Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<stdin>", line 3, in __new__
  TypeError: haha
  >>>

But how would you prevent this?

  >>> a = C()
  >>> a.__class__ = D
  >>>

--Guido van Rossum (home page: http://www.python.org/~guido/)