Some minor updates to reflect the removal of .clear(). Added information

(preliminary) about proxy objects.
This commit is contained in:
Fred Drake 2001-01-22 22:11:03 +00:00
parent bd39cf0ae3
commit 1f0aa4313d
1 changed files with 26 additions and 12 deletions

View File

@ -74,11 +74,13 @@ Proposed Solution
references to external resources (database connections, open references to external resources (database connections, open
files, etc.). files, etc.).
A new module, weakref, will contain two new functions used to A new module, weakref, will contain new functions used to create
create weak references. weakref.new() will create a "weak weak references. weakref.new() will create a "weak reference
reference object" and optionally attach a callback which will be object" and optionally attach a callback which will be called when
called when the object is about to be finalized. the object is about to be finalized. weakref.mapping() will
weakref.mapping() will create a "weak dictionary". create a "weak dictionary". A third function, weakref.proxy(),
will create a proxy object that behaves somewhat like the original
object.
A weak reference object will allow access to the referenced object A weak reference object will allow access to the referenced object
if it hasn't been collected and to determine if the object still if it hasn't been collected and to determine if the object still
@ -99,13 +101,25 @@ Proposed Solution
to be finalized), and will be unregistered from the object. Like to be finalized), and will be unregistered from the object. Like
dictionaries, weak dictionaries are not hashable. dictionaries, weak dictionaries are not hashable.
The callbacks registered with weak reference objects or in weak Proxy objects are weak references that attempt to behave like the
dictionaries must accept a single parameter, which will be the object they proxy, as much as they can. Regardless of the
weak-ly referenced object itself. The object can be resurrected underlying type, proxies are not hashable since their ability to
by creating some other reference to the object in the callback, in act as a weak reference relies on a fundamental mutability that
which case the weak reference generating the callback will still will cause failures when used as dictionary keys -- even if the
be cleared but no weak references remaining to the object will be proper hash value is computed before the referent dies, the
cleared. resulting proxy cannot be used as a dictionary key since it cannot
be compared once the referent has expired, and comparability is
necessary for dictionary keys. Operations on proxy objects after
the referent dies cause weakref.ReferenceError to be raised in
most cases. "is" comparisons, type(), and id() will continue to
work, but always refer to the proxy and not the referent.
The callbacks registered with weak references must accept a single
parameter, which will be the weak-ly referenced object itself.
The object can be resurrected by creating some other reference to
the object in the callback, in which case the weak reference
generating the callback will still be cleared but no remaining
weak references to the object will be cleared.
Implementation Strategy Implementation Strategy