[ZODB-Dev] use PersistentList and PersistenDict when?

Jeremy Hylton jeremy@zope.com (Jeremy Hylton)
Tue, 10 Dec 2002 12:47:27 -0500


There are a couple of slides about this in my talk from LL2.
(http://www.python.org/~jeremy/talks.html)

The bulleted summary:

Pickling persistent objects
    * Stores objects in separate records
          o Persistent objs pickled as oid + class
          o Works with cache to maintain identity
    * Handling non-persistent objects
          o Copied into record of containing object
          o Sharing by persistent objs is problematic 

Here's some text that fleshes this out:

Objects are serialized using the standard Python pickle format,
implemented by the pickle and cPickle modules.  The pickle library
handles most Python datatypes, including numbers, strings, and class
instances.  Some objects are intentionally unpicklable -- file and
socket objects, tracebacks, code objects.  It provides hooks that
allow persistent object to be detected during serialization and
handled differently.

A persistent object, i.e. one that inherits from `Persistent`, is
serialized along with all non-persistent objects reachable from it.
The pickle module allows objects to customize their serialized
representation using the special `__getstate__` and `__setstate__`
methods.  ZODB also ignores attribute names that begin with _v_.

Setting an attribute marks a persistent object as changed.  When a
non-persistent sub-object is modified, the special _p_changed
attribute must be set to mark the containing persistent object as
changed.

The serialization mechanism is simple, but the programmer must be
careful to avoid sharing non-persistent objects, like builtin lists
and dictionaries.  A builtin list is pickled by value.  If two
different persistent objects reference the same list, they will each
get a copy of the list when they are serialized.  When the objects are
later loaded from the database, they will independent copies of the
list.

Jeremy