Skip to content

Tag: immutability

Why true immutability is impossible in Python?

I was reading the documentation for attrs. It says: Please note that true immutability is impossible in Python I am wondering what is the reason for that. Why someone cannot have an immutable list in Python while it is possible in C++? What is the main difference here? Answer TLDR; “True Immutability&#8…

Equality between frozensets

Example: Frozen sets are immutable. I would expect that equality between immutable objects should by determined by order, but here obviously that is not the case. How can I discard frozensets with same elements and different order, without casting to different type? Answer The short answer is you can’t,…

Immutable numpy array?

Is there a simple way to create an immutable NumPy array? If one has to derive a class from ndarray to do this, what’s the minimum set of methods that one has to override to achieve immutability? Answer You can make a numpy array unwriteable: Also see the discussion in this thread: http://mail.scipy.org…

How to make an immutable object in Python?

Although I have never needed this, it just struck me that making an immutable object in Python could be slightly tricky. You can’t just override __setattr__, because then you can’t even set attributes in the __init__. Subclassing a tuple is a trick that works: But then you have access to the a and…