Replace items in Python's "immutable" tuples.
yolo
We use Rust's PyO3 crate to manipulate tuples directly using the Stable CPython API.
This project is just for fun. Do not use mutuple in production!
import mutuple
def test_mutable_tuple():
"""mutuple tuple demo"""
value = ('read-only', 'tuple')
mutuple.setitem(value, 0, 'mutable')
assert value == ('mutable', 'tuple')
Modifying tuples is a wild thing to do in Python.
While there are (questionably?) safe ways to use this package, there many things that can go wrong when miused. This is not an exhaustive list of gotchas.
-
Editing a tuple changes its
hash(...)
value. This means that bad things will happen if you edit a tuple after adding it to an associative container (i.e.set
objects anddict
keys). -
Python
set
containers expect that thehash(...)
value of an object does not change after the object is added to aset
. Editing a tuple effectively changes itshash()
value, which "breaks" python and creates aset
with atuple
in it that cannot be removed! -
Editing a tuple after using it as a key in a
dict
behaves the same way ~ the tuple's entry cannot be removed from thedict
. This is also due to the fact that the tuple'shash(...)
value changed after adding it to the container.
mutuple
is feature complete and stable. New features will not be added outside of
corrections to the implementation, packaging, documentation and test suite.