aio-libs / frozenlist

`FrozenList` is a `list`-like structure that implements `collections.abc.MutableSequence` and can be made immutable.

Home Page:https://frozenlist.aio-libs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ability to unfreeze collection after `.freeze()` method is called

Gr1N opened this issue · comments

Long story short

Hi,

In some cases, I need to unfreeze my collection, make some mutation, and freeze it again. With the current API of the collection, there is no way to do that without collection recreation.

I can submit a PR with changes I want to see in the library, just let me know if my proposal is acceptable.

Expected behaviour

l = FrozenList()
l.append(1)
l.freeze()

l.unfreeze()
l.append(2)

Actual behaviour

l = FrozenList()
l.append(1)
l.freeze()

l = FrozenList()
l.append(1)
l.append(2)

Steps to reproduce

Described in the examples above.

Your environment

With the current API of the collection, there is no way to do that without collection recreation.

That's deliberate. Once frozen, it it is an immutable collection, like a tuple or a frozenset. Other code can then rely on the sequence to never change; it can be used in concurrent code.

You can't create an unfreeze() method without breaking this, so is not something we'll add here.

Instead, create a new FrozenList() that is a copy of the old, frozen FrozenList() instance, and replace the reference. That way existing references to the frozen copy can complete their work, then later on grab a reference to the new object.