This package provides backports of new features in Python's standard `weakref` module under the `backports` namespace. Its primary backported functionality is `weakref.finalize`, which was introduced in Python 3.4. The library, currently at version `1.0.post1`, was last released in September 2017 and is primarily useful for Python 2.7 and Python 3 versions up to 3.6, as newer Python versions include these features natively.
pip install backports-weakrefVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use the backported `finalize` to ensure a cleanup function is called when an object is garbage collected. This is particularly useful for managing external resources like file handles or network connections.
For Python 3.4+ environments, use `import weakref` directly, as the functionality is built-in.
Ensure the object type supports weak references. Custom classes (without `__slots__` preventing `__weakref__`) and certain built-in types (e.g., instances of user-defined classes, functions) generally work.
Ensure finalizer callbacks are robust and handle their own internal errors to prevent silent failures. Avoid complex or potentially error-prone logic within callbacks.
Check your Python version. For Python 3.4+, use the native `weakref.WeakMethod`. For older Python versions, if `WeakMethod` is required, a different dedicated backport or a custom implementation might be necessary.
Ensure the package is installed using pip: `pip install backports-weakref`.
Install the package: `pip install backports-weakref`. If already installed, check your Python path and virtual environment.
Reinstall the package to ensure all components are correctly placed: `pip uninstall backports-weakref && pip install backports-weakref`.
Verify that `backports-weakref` is installed and up-to-date (`pip install --upgrade backports-weakref`). Also, double-check the import statement for typos: `from backports.weakref import finalize`.
No dependency data recorded yet.