weakrefs and things that point to each other

Jonathan Gardner gardner at cardomain.com
Fri Jun 22 14:47:18 EDT 2001


Duncan Booth wrote:
> Jonathan Gardner <gardner at cardomain.com> wrote in
> news:9gu4eg$838$1 at brokaw.wa.com:
> 
>> Okay, here''s the idea. Object A can have many things of Class B.
>> Object B can be had by many things of Class A. When A is destroyed, it
>> wants to let B know that it no longer has it. When B is destroyed, it
>> wants to let A know it no longer should have it.
>> 
>> But, because of circular references, we need to use weak refs on at
>> least half of the equations. However, I want the B to destroy itself
>> even when there is one or more A's pointing to it. So I will implement
>> weakrefs on both sides.
>
> If you have weak references on both sides then are you sure something is
> going to keep all those bees alive?

Yes, the bees will be kept alive by other references. I just don't want 
them to be kept alive by each other when they are no longer referenced by 
something else.

This is actually going to be a simple signal/slot thingy like Qt. You'd do 
something like this to use it:

from sigslot import *

def doit(*args):
    print "Did it."

def dothis(*args):
    print "Did this"

a = signal()
b = slot(doit)
a.connect(b) # or b.connect(a)

a.emit() # prints "Did it."

c = slot(dothis)
a.connect(c)

a.emit() # print "Did it." then "Did this."

The only thing that should keep a signal or slot alive is a reference to 
it. When a, b, or c falls out of scope, they should all individually be 
completely destroyed and all traces should be removed. That means when a is 
gone, b and c will forget it ever existed. When b or c is gone, a should 
forget about them as well.

-- 
Jonathan Gardner
"Infinity isn't all that large - except at the end." -- Uncle Al

Please don't take anything I say seriously, even if you are really gullible.



More information about the Python-list mailing list