Skip to content
Advertisement

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” is only possible on an impervious stone tablet,

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, since as pointed out in the comments, sets and frozensets are

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/pipermail/numpy-discussion/2008-December/039274.html and the documentation: http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.flags.html

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 b variables through self[0] and self[1], which

Advertisement