Persistence

5.4 · active · verified Thu Apr 16

The `Persistence` package (version 5.4) provides a specific variant of the persistent base class, `Persistent`, implemented as an ExtensionClass. It is part of the Zope Foundation ecosystem and is primarily intended for use when ExtensionClass semantics are required. For general-purpose object persistence without these specific semantics, the `persistent.Persistent` class from the `persistent` distribution is typically recommended. The library is mature and receives occasional updates, with its most recent release in November 2025.

Common errors

Warnings

Install

Imports

Quickstart

Defines a basic persistent class using `Persistence.Persistent`. Note that for true persistence, this class needs to be instantiated and managed within a ZODB (Zope Object Database) context, which typically involves setting up a storage and connection.

from Persistence import Persistent

class MyPersistentObject(Persistent):
    def __init__(self, value):
        self.value = value
        self.data = []

    def add_data(self, item):
        self.data.append(item)

# In a real ZODB application, this object would be managed by a Connection/Database.
# Example: (requires ZODB setup, not runnable standalone for full persistence)
# from ZODB import DB, FileStorage
# import transaction
#
# storage = FileStorage.FileStorage('my_data.fs')
# db = DB(storage)
# conn = db.open()
# root = conn.root()
#
# if 'my_obj' not in root:
#     root['my_obj'] = MyPersistentObject('initial_value')
#     transaction.commit()
#
# obj = root['my_obj']
# print(f'Initial value: {obj.value}, Data: {obj.data}')
# obj.add_data('new_item')
# obj.value = 'updated_value'
# transaction.commit()
#
# print(f'Updated value: {obj.value}, Data: {obj.data}')
# conn.close()
# db.close()

view raw JSON →