Circular references and python

Brian Dam Pedersen brian.pedersen at mail.danbbs.dk
Wed Feb 2 03:54:10 EST 2000


Jason Stokes <jstok at bluedog.apana.org.au> wrote in message
news:04Sl4.16542$3b6.68582 at ozemail.com.au...
> Well, my adventures with the Python language are moving along.  One thing
> that worries me is the lack of garbage collection in Python; the
application
> I'm thinking of using Python for demands many data structures that use
> circular references, but Python only implements a reference counting
scheme
> in the way of memory management.  How do I use such data structures in
> Python?
>

You can use circular references just as in any other language, you just has
to do some manual cleanup on your lists. See the following

a=[]
b=[]
a.append(b)
b.append(a)

Now the lists are circular referenced, and dereferencing them will not
garbage-
collect them. To break up the chain just do

a[0]=None

or

del(a[0])

which will allow the garbage collection to work.

--
Brian Dam Pedersen
Software Engineer






More information about the Python-list mailing list