01.03.12 16:47, André Malo написав(ла):
> On Thursday 01 March 2012 15:17:35 Serhiy Storchaka wrote:
>> This is the first rational use of frozendict that I see. However, a deep
>> copy is still necessary to create the frozendict. For this case, I
>> believe, would be better to "freeze" dict inplace and then copy-on-write
>> it.
> In my case it's actually a half one. The data mostly comes from memcache ;)
> I'm populating the object and then I'm done with it. People wanting to modify
> it, need to copy it, yes. OTOH usually a shallow copy is enough (here).
What if people modify dicts in deep?
a = frozendict({1: {2: 3}})
b = a.copy()
c = a.copy()
assert b[1][2] == 3
c[1][2] = 4
assert b[1][2] == 4
You need to copy incoming dict in depth.
def frozencopy(value):
if isinstance(value, list):
return tuple(frozencopy(x) for x in value)
if isinstance(value, dict):
return frozendict((frozencopy(k), frozencopy(v)) for k, v in
value.items())
return value # I'm lucky
And when client wants to modify the result in depth it should call
"unfrozencopy". Using frozendict profitable only when multiple clients are
reading the result, but not modify it. Copy-on-write would help in all cases
and would simplify the code. But this is a topic for python-ideas, sorry.
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com