The `persistent` library provides a base class, `Persistent`, for creating translucent persistent Python objects. These objects can track their their own 'dirty' state, indicating when they have been modified and need to be saved by an external persistence layer (e.g., ZODB). It is a fundamental component for building object persistence systems. The current version is 6.5, with major releases typically aligning with Python version support updates and addressing core behavior changes.
pip install persistentVerified import paths — ran on the pinned version, not inferred.
Demonstrates the basic usage of `persistent.Persistent` by subclassing it and observing how its `_p_changed` attribute is affected by direct attribute assignments. It also highlights a common gotcha regarding changes to mutable attributes within the object when not managed by a full persistence layer.
Upgrade to Python 3.8 or newer to use `persistent` 6.x. For older Python versions, pin `persistent` to an earlier major version (e.g., `persistent<5` for Python 2.7, `persistent<6` for Python 3.7).
Code that relied on `isinstance(obj.__dict__, _PersistentDict)` or specific `_PersistentDict` methods will break. Treat `obj.__dict__` as a standard Python dictionary.
Either reassign the mutable attribute (e.g., `obj.my_list = new_list`), manually set `obj._p_changed = True` after modifying the internal mutable object, or ensure the object is managed by a full persistence layer (like ZODB) that provides proxy objects for mutable attributes.
Understand that `persistent` is a low-level building block. To persist objects to disk or a database, you will need to integrate it with a suitable persistence framework (e.g., ZODB).
The correct way to import the `Persistent` class is `from persistent import Persistent` (lowercase `persistent` for the module, uppercase `Persistent` for the class).
If you need to use `Persistent` objects as dictionary keys or set members, you must implement `__hash__` and `__eq__` methods in your persistent class. Alternatively, avoid using `Persistent` objects directly in hash-based collections if identity-based comparisons are sufficient, as their object identity remains consistent across database connections.
Ensure that `Persistent` objects are created, loaded, and manipulated within the scope of a ZODB database connection. If this error persists within a ZODB context, verify the health of your database and the lifecycle management of your persistent objects.
Check for data corruption in your storage (e.g., ZODB data file). Ensure that the same Python and `persistent` library versions are used consistently for both saving and loading data. If class definitions have evolved, implement schema migration logic or `__setstate__`/`__getstate__` methods in your persistent classes to handle backward compatibility. Verify the `pickle` protocol version in use, especially when migrating between older and newer Python/ZODB versions.
No dependency data recorded yet.