changes to shelf items fail silently?
Alex Martelli
aleax at aleax.it
Tue Mar 18 02:53:27 EST 2003
eichin at metacarta.com wrote:
>> They're mutating the returned list (to which you retain no
>> references, and which therefore is immediately discarded).
>
> Ah, so I'm missing the distinction that it's returning a list, and not
> being a reference to one. (Knowable only by looking at the code to
> shelve, since the syntax hides it, right?)
Right. I consider this a bug in shelve, myself.
>> 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
>
> Or just pass back wrapped-objects, whose methods are "like" the input
> type but commit themselves on __setitem__ calls?
Unfortunately insufficient -- there are a zillion more ways to mutate
shelved objects in addition to __setitem__ calls. For lists, you could
go through the list of operations and methods and religiously wrap
every single one that's a mutator. But then you'd have to do it all
over again for dicts, and (the kicker...) for every mutable type (an
infinity thereof, mostly instances of classic and new-style classes).
> In the meantime, I've worked around it with explicit stores, but
> eventually will want something cleaner.
It _would_ be nice if there was a general way for objects to expose
an "I'm about to mutate" hook -- it would make such wrapping quite
feasible and also allow a general solution to the issue of making
immutable snapshots of something (wrap that thing with a wrapper that
raises a suitable exception when mutation would be about to happen).
Unfortunately, so far we have no design yet for such a feature:-(.
Alex
More information about the Python-list
mailing list