Reference count access or better method?

Alex Martelli aleax at aleax.it
Sun Jan 13 18:17:45 EST 2002


Philip Swartzleonard wrote:
        ...
> continual access. When no one else but the dictionary refernces the
> object, i want to have the _option_ to purge it (just giving up on it and
> deleting it from the dict is not what i want, it must have a reference
        ...
> Is weekref what i want for something like this? Is there a better way to

Almost.  weakref.ref and .proxy do let you specify a callback, but that
happens when the referent is no longer available.  You could wrap the
referent into a highly-transparent-proxy, have all client code refer to
the proxy, and when the proxy goes away and your weakref.ref to it
triggers your callback, you still have the real object in hand.  I.e., 
"there's no ill that can't be cured by another level of indirection":-).

The highly transparent proxy can be built by automatic delegation, cfr
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52295


Alex




More information about the Python-list mailing list