Questions tagged [immutability]

Immutability is the inability to modify data after it has been created. Modifications are instead made by copying the data. A property of immutable data is that it is *referentially transparent*.

Overview

Immutability is the inability to modify a variable after it is has been created.

It is a pattern found in many branches of programming; immutable objects are used widely within object oriented languages (such as Python's str type, Java's String and Integer type, .NET's System.String, etc.), functional programming (esp. Haskell and other pure languages), and other paradigms.

See also

3670 questions
133
votes
22 answers

Aren't Python strings immutable? Then why does a + " " + b work?

My understanding was that Python strings are immutable. I tried the following code: a = "Dog" b = "eats" c = "treats" print a, b, c # Dog eats treats print a + " " + b + " " + c # Dog eats treats print a # Dog a = a + " " + b + " " + c print a #…
jason
  • 5,391
  • 6
  • 23
  • 26
127
votes
7 answers

Java Immutable Collections

From Java 1.6 Collection Framework documentation: Collections that do not support any modification operations (such as add, remove and clear) are referred to as unmodifiable. [...] Collections that additionally guarantee that no change in the…
Bhaskar
  • 7,443
  • 5
  • 39
  • 51
127
votes
7 answers

Does Python have an immutable list?

Does python have immutable lists? Suppose I wish to have the functionality of an ordered collection of elements, but which I want to guarantee will not change, how can this be implemented? Lists are ordered but they can be mutated.
cammil
  • 9,499
  • 15
  • 55
  • 89
126
votes
8 answers

How do I create an immutable Class?

I am working on creating an immutable class. I have marked all the properties as read-only. I have a list of items in the class. Although if the property is read-only the list can be modified. Exposing the IEnumerable of the list makes it…
Biswanath
  • 9,075
  • 12
  • 44
  • 58
116
votes
10 answers

Replace Multiple String Elements in C#

Is there a better way of doing this... MyString.Trim().Replace("&", "and").Replace(",", "").Replace(" ", " ") .Replace(" ", "-").Replace("'", "").Replace("/", "").ToLower(); I've extended the string class to keep it down to one job but…
Chris McKee
  • 4,298
  • 10
  • 48
  • 83
114
votes
12 answers

Is Integer Immutable

I know this is probably very stupid, but a lot of places claim that the Integer class in Java is immutable, yet the following code: Integer a=3; Integer b=3; a+=b; System.out.println(a); Executes without any trouble giving the (expected) result 6.…
K.Steff
  • 2,164
  • 3
  • 19
  • 25
111
votes
5 answers

val-mutable versus var-immutable in Scala

Are there any guidelines in Scala on when to use val with a mutable collection versus using var with an immutable collection? Or should you really aim for val with an immutable collection? The fact that there are both types of collection gives me a…
James McCabe
  • 1,879
  • 2
  • 15
  • 22
110
votes
10 answers

What does immutable mean?

If a string is immutable, does that mean that.... (let's assume JavaScript) var str = 'foo'; alert(str.substr(1)); // oo alert(str); // foo Does it mean, when calling methods on a string, it will return the modified string, but it won't change…
alex
  • 479,566
  • 201
  • 878
  • 984
102
votes
4 answers

If Python strings are immutable, why does it keep the same id if I use += to append to it?

Strings in Python are immutable, which means the value cannot be changed. However, when appending to the string in the following example, it looks like the original string memory is modified since the id remains the same: >>> s = 'String' >>> for i…
Master_Roshy
  • 1,245
  • 3
  • 12
  • 19
99
votes
9 answers

Hashable, immutable

From a recent SO question (see Create a dictionary in python which is indexed by lists) I realized I probably had a wrong conception of the meaning of hashable and immutable objects in python. What does hashable mean in practice? What is the…
joaquin
  • 82,968
  • 29
  • 138
  • 152
96
votes
3 answers

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?
NPE
  • 486,780
  • 108
  • 951
  • 1,012
96
votes
2 answers

Isn't Redux just glorified global state?

So I started learning React a week ago and I inevitably got to the problem of state and how components are supposed to communicate with the rest of the app. I searched around and Redux seems to be the flavor of the month. I read through all the…
Ryan Peschel
  • 11,087
  • 19
  • 74
  • 136
96
votes
9 answers

Building big, immutable objects without using constructors having long parameter lists

I have some big (more than 3 fields) objects that can and should be immutable. Every time I run into that case I tend to create constructor abominations with long parameter lists. It doesn't feel right, it is hard to use, and readability…
Malax
  • 9,436
  • 9
  • 48
  • 64
95
votes
20 answers

Why do we need immutable class?

I am unable to get what are the scenarios where we need an immutable class. Have you ever faced any such requirement? or can you please give us any real example where we should use this pattern.
Rakesh Juyal
  • 35,919
  • 68
  • 173
  • 214
95
votes
11 answers

Why would one declare an immutable class final in Java?

I read that to make a class immutable in Java, we should do the following, Do not provide any setters Mark all fields as private Make the class final Why is step 3 required? Why should I mark the class final?
Anand
  • 20,708
  • 48
  • 131
  • 198