Problem with shelve / nested lists

Fredrik Lundh fredrik at pythonware.com
Sun Jul 14 07:12:00 EDT 2002


Michael Schmitt wrote:

> I'm trying to use a shelve as a disc based dictionary, but have problems
> with nested lists. If I store a nested list as value, inner lists seem to
> be immutable.
>
> According to the library reference, shelves can store "essentially
> arbitrary Python objects" and "recursive data types".

the shelve only stores things when you write

    d1['bla'] = value

and only fetches things when you use d['bla']
in an expression.


if you write

    d1['bla'][2].append(30)

you'll fetch a value from the shelve, modify the
return value, but you're not writing it back.


(with this in mind, this isn't that different from
asking why, say

    file.read(20) + "hello"

doesn't write "hello" to the file)


to make this work as you want, think in read/write terms,
and make sure you always assign to the shelve when you've
modified a value, so it can write it back:

    value = d1['bla']
    value[2].append(30)
    d1['bla'] = value

</F>





More information about the Python-list mailing list