python-frozendict icon indicating copy to clipboard operation
python-frozendict copied to clipboard

Thoughts on modifiable_copy?

Open yuanxu-li opened this issue 6 years ago • 2 comments

from frozendict import frozendict
a = frozendict({'a': 1})
b = a.copy()
b['c'] = 1

throws the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'frozendict' object does not support item assignment

However, a common usage in my program is to copy over the unmodifiable frozendict to another place and do some modification. I wonder if we can provide a method called modifiable_copy such that

from frozendict import frozendict
a = frozendict({'a': 1})
b = a.modifiable_copy()
b['c'] = 1

is allowed

yuanxu-li avatar Aug 06 '18 17:08 yuanxu-li

>>> import frozendict
>>> write_protected = frozendict.frozendict(a='foo', b='bar')
>>> write_enabled = dict(write_protected)
>>> write_protected
<frozendict {'b': 'bar', 'a': 'foo'}>
>>> write_enabled
{'b': 'bar', 'a': 'foo'}
>>> write_enabled['c'] = 'baz'
>>> write_enabled
{'b': 'bar', 'c': 'baz', 'a': 'foo'}
>>> 

ppolewicz avatar Aug 06 '18 21:08 ppolewicz

Yes, there's no need of another method, you can simply convert to dict.

Marco-Sulla avatar Aug 02 '19 13:08 Marco-Sulla