Seite 1 von 1

dicts im shelve?

Verfasst: Mittwoch 30. Juli 2008, 14:30
von Rebecca
Warum geht folgendes nicht?

Code: Alles auswählen

>>> s = shelve.open("/tmp/s")
>>> s["a"] = {}
>>> s["a"]["b"] = 42
>>> s
{'a': {}}
>>> s["a"]["b"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'b'
>>> s["a"]
{}
So wie ich die Doku verstehe, sollte es funktionieren:
shelve Doku hat geschrieben:values (not the keys!) in a shelf can be essentially arbitrary Python objects -- anything that the pickle module can handle
pickle Doku hat geschrieben:The following types can be pickled:
* None, True, and False
* integers, long integers, floating point numbers, complex numbers
* normal and Unicode strings
* tuples, lists, sets, and dictionaries containing only picklable objects
[...]

Verfasst: Mittwoch 30. Juli 2008, 14:42
von sma
Die Doku sagt:
Normally, d[key] returns a COPY of the entry. This needs care when mutable entries are mutated: for example, if d[key] is a list, d[key].append(anitem) does NOT modify the entry d[key] itself, as stored in the persistent mapping
Stefan

Verfasst: Mittwoch 30. Juli 2008, 14:55
von Rebecca
:idea: Danke!