expired()
Checks if the target object of a dbzero.weak_proxy has been garbage-collected.
This method is the counterpart to dbzero.weak_proxy(). After creating a weak reference to an object, you can use expired() to determine if the original object still exists in the store or if it has been removed.
Parameters
- proxy_object (
dbzero.proxy): The weak proxy object, returned fromdbzero.weak_proxy(), whose target you want to check.
Returns
bool: ReturnsTrueif the object referenced by the proxy no longer exists. ReturnsFalseif the object is still present in the store.
Example
Here’s a typical lifecycle: create an object, create a weak proxy to it, delete the original object's strong references, and then check the proxy's status.
# Create an object that will be our target
original_obj = MyClass(value=123)
# Create another object that holds a weak proxy to the first one
wrapper_obj = OtherClass(ref=db0.weak_proxy(original_obj))
# The weak proxy is not expired yet, as original_obj still exists
assert not db0.expired(wrapper_obj.ref)
# Now, delete the only strong reference to the original object
del original_obj
db0.commit() # This will garbage-collect original_obj
# The proxy is now expired because its target is gone
assert db0.expired(wrapper_obj.ref) is True💡
Expired Proxies Can Still Be Used for Lookups
An interesting feature of dbzero is that even when a weak proxy is expired, it still retains the identity of the original object. This means you can use an expired proxy to find other objects that were tagged with the original object's identity.
# ... continuing from the example above ...
# Tag another object using the now-expired proxy
some_other_obj = AnotherClass()
db0.tags(some_other_obj).add(db0.as_tag(wrapper_obj.ref))
db0.commit()
# We can still find the tagged object using the expired proxy as a lookup key
found_objs = list(db0.find(db0.as_tag(wrapper_obj.ref)))
assert found_objs == [some_other_obj]