Python bugs

Skip Montanaro skip at mojam.com
Mon Nov 29 15:39:45 EST 1999


    >>> s = shelve.open("foo")
    >>> l = []
    >>> s["bar"] = l
    >>> s["bar"]
    []
    >>> l.append("baz")
    >>> s["bar"]
    []

I think you're asking for magic that really can't happen (at least not
easily).  Shelve transparently does the mapping to strings that most/all
dbm-like packages require, so when you execute

    s["bar"] = l

you'd don't get the usual object-to-name binding you expect.  Instead, l is
marshalled into Pickle's string form and that string is stuffed into the
underlying dbm file keyed by the string "bar".  Similarly, when getting the
value of s["bar"], the read+unpickle operation is performed.

When you append "baz" to l, it has nothing to do with s["bar"].  While this
isn't as transparent as you'd like, the lack of transparency has one big
advantage: speed.  Because you're forced to explicitly read and write
elements of a shelve object, e.g.:

    s = shelve.open("foo")
    l = s["bar"]
    ... do lots of stuff with l ...
    s["bar"] = l

instead of

    s = shelve.open("foo")
    ... do lots of stuff with s["bar"] ...

the result is that all operations involving l are done in memory instead of
by constanting marshalling and unmarshalling a possibly very big item out of
a file.

All that aside, I believe the good folks at Digital Creations worked some
magic using ExtensionClasses a few years ago to make this work in certain
contexts.

Skip Montanaro | http://www.mojam.com/
skip at mojam.com | http://www.musi-cal.com/
847-971-7098   | Python: Programming the way Guido indented...




More information about the Python-list mailing list