AST structures and memory leaks

Gordon McMillan gmcm at
Thu Mar 16 15:05:32 CET 2000

Robin Becker wrote:

> I want to have an AST container A contain instances of B and would like
> the instances to know who contains them, but can't seem to avoid memory
> leaks.
> The general structure is
> class A:
>     def __init__(self):
>         self.list=[]
>     def add(self,b1):
>         self.list.append(b)
> class B:
>     def __init__(self,owner):
>         self.owner=owner
> a=A()
> a.add(B(a))
> which is clearly loopy :)
> Is there any way to find the 'owner' of an instance?

The general case would probably be:

class AST:
  def __init__(self, owner, kids=None):
    self.owner = owner = kids
    if kids is None: = []
  def add(self, kid):
  def cleanup(self):
    for kid in
    self.owner = None

...which obviously requires an explicit call to root.cleanup() 
when you're done.

I-suppose-I-could-charge-Andy-for-that-<wink>-ly y'rs

- Gordon

More information about the Python-list mailing list