Destructors and reference counting

Terry Reedy tjreedy at udel.edu
Wed Jul 7 00:29:37 CEST 2004


"Christopher T King" <squirrel at WPI.EDU> wrote in message
news:Pine.LNX.4.44.0407061530130.26902-100000 at ccc4.wpi.edu...
> On 6 Jul 2004, Elbert Lev wrote:
>
> > Please correct me if I'm wrong.
> > Python (as I understand) uses reference counting to determine when to
> > delete the object. As soon as the object goes out of the scope it is
> > deleted. Python does not use garbage collection (as Java does).
>
> True, except Python uses garbage collection in the sole case of reference
> cycles (which you will likely not encounter).

As I mentioned in my response to the OP, this is all specific to the
CPython implementation and not true of Python the language.

> > So if the script runs a loop:
> >
> > for i in range(100):
> >     f = Obj(i)
> >
> > Every time f = Obj(i) is executed it is preceded by f.__del__() call.
> >
> > Is the same true for functions like:
> >
> > def A():
> >     f1 = Obj(1)
> >     f2 = Obj(2)
> >
> > Are f1 and f2 deleted before returning from A?
>
> True on all counts.

Wrong on both counts, at least in the sense of 'not always true and
misleadingly stated',  as I explained in my main response, even for
CPython.

Terry J. Reedy






More information about the Python-list mailing list