changes to shelf items fail silently?

Steven Taschuk staschuk at
Tue Mar 18 02:53:42 CET 2003

Quoth eichin at
> complex_example["a"].append("b")
> complex_example["a"].extend(["b"])
> complex_example["a"] = complex_example["a"] + ["b"]
> So, I see why the last case works (it can't help *but* work) but I
> don't get why the append/extend mutators don't.  Or rather, why if
> they can't work, I don't get an error thrown so I notice it...

They're mutating the returned list (to which you retain no
references, and which therefore is immediately discarded).

This would work if you were working with a dict, since then the
returned list would be the same object as the object in the dict. 
But since it's a shelve, the returned list is just an in-memory
copy of the object in the shelve, and changes to it do not affect
the shelve.

Your append example, e.g., is the same as doing
	foo = complex_example["a"]
There's no assignment into complex_example, so the shelve doesn't
get updated.  Doing
	foo = complex_example["a"]
	complex_example["a"] = foo
will certainly work, and
	complex_example["a"] += ["b"]
might too.

> (this occurs in 2.1 and 2.2.  Is there a "better" shelf-like thing
> that I should use instead, for more completely persistent data?)

You want *every* change to the object to cause the shelve to be
updated immediately?  Really?

That's pretty hard in general, since the shelve-like thing can't
know a priori what operations mutate an object and what operations
don't.  (It could make an effort to detect changes after the fact
by checking for changes in __dict__ or in __getstate__ or
__reduce__'s return value... but you'd have to do this after
*every* method call on the object.  Ick.)

Steven Taschuk                               staschuk at
"What I find most baffling about that song is that it was not a hit."
                                          -- Tony Dylan Davis (CKUA)

More information about the Python-list mailing list