Use __del__ in Python to clean up Rust objects #61
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
__del__
more closely maps to what we want than context managers. Forinstance, with the current code's use of context management, the only way to
make a collection of
ZipCodeDatabase
s would be to explicitly nestwith ... as
statements. As well, a context manager would complicatepersisting a Rust object that is expensive to initialize but cheap to use,
whereas
__del__
supports this while still freeing the resources when thereference count drops to zero.
There are two drawbacks to using
__del__
over context management:predictable.
of. The fact that some of the objects involved will be cleaned up by
calling a Rust function doesn't exacerbate that problem.
out of scope
is any different than any other allocated object in Python. Additionally,
we know in the Rust world to not rely on
drop
being called to guaranteeproperties like safety, so I think
__del__
maps better to the concept ofthe
drop
function it will be calling.Aside from
__del__
reducing the restrictions on using Rust objects withinPython, it's also conceptually simpler. And to an audience that's already
learning Rust, it should be easier to teach in conjunction with teaching
Drop
.Fixes #60