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
707
votes
26 answers

Remove specific characters from a string in Python

I'm trying to remove specific characters from a string using Python. This is the code I'm using right now. Unfortunately, it appears to do nothing to the string. for char in line: if char in " ?.!/;:": line.replace(char,'') How do I do…
Matt Phillips
  • 11,249
  • 10
  • 46
  • 71
653
votes
12 answers

Why are exclamation marks used in Ruby methods?

In Ruby some methods have a question mark (?) that ask a question like include? that ask if the object in question is included, this then returns a true/false. But why do some methods have exclamation marks (!) where others don't? What does it mean?
Lennie
  • 10,605
  • 5
  • 22
  • 13
553
votes
16 answers

Why are mutable structs “evil”?

Following the discussions here on SO I already read several times the remark that mutable structs are “evil” (like in the answer to this question). What's the actual problem with mutability and structs in C#?
Dirk Vollmar
  • 172,527
  • 53
  • 255
  • 316
422
votes
17 answers

What is meant by immutable?

What exactly does immutable mean - that is, what are the consequences of an object being mutable or immutable? In particular, why are Java's Strings immutable? My understanding is that the StringBuilder type is something like a mutable equivalent to…
ashokgelal
  • 80,002
  • 26
  • 71
  • 84
413
votes
16 answers

Is a Java string really immutable?

We all know that String is immutable in Java, but check the following code: String s1 = "Hello World"; String s2 = "Hello World"; String s3 = s1.substring(6); System.out.println(s1); // Hello World System.out.println(s2); // Hello World …
Darshan Patel
  • 3,176
  • 6
  • 26
  • 49
388
votes
18 answers

Const in JavaScript: when to use it and is it necessary?

I've recently come across the const keyword in JavaScript. From what I can tell, it is used to create immutable variables, and I've tested to ensure that it cannot be redefined (in Node.js): const x = 'const'; const x = 'not-const'; // Will give an…
axdg
  • 4,025
  • 3
  • 13
  • 7
286
votes
12 answers

What is the difference between shallow copy, deepcopy and normal assignment operation?

import copy a = "deepak" b = 1, 2, 3, 4 c = [1, 2, 3, 4] d = {1: 10, 2: 20, 3: 30} a1 = copy.copy(a) b1 = copy.copy(b) c1 = copy.copy(c) d1 = copy.copy(d) print("immutable - id(a)==id(a1)", id(a) == id(a1)) print("immutable - id(b)==id(b1)",…
deeshank
  • 4,286
  • 4
  • 26
  • 32
278
votes
15 answers

Correct way to push into state array

I seem to be having issues pushing data into a state array. I am trying to achieve it this way: this.setState({ myArray: this.state.myArray.push('new value') }) But I believe this is incorrect way and causes issues with mutability?
Ilja
  • 44,142
  • 92
  • 275
  • 498
273
votes
13 answers

Why is immutability so important (or needed) in JavaScript?

I am currently working on React JS and React Native frameworks. On the half way road I came across Immutability or the Immutable-JS library, when I was reading about Facebook's Flux and Redux implementation. The question is, why is immutability so…
bozzmob
  • 12,364
  • 16
  • 50
  • 73
249
votes
27 answers

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…
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
237
votes
26 answers

Immutability of Strings in Java

Consider the following example. String str = new String(); str = "Hello"; System.out.println(str); //Prints Hello str = "Help!"; System.out.println(str); //Prints Help! Now, in Java, String objects are immutable. Then how come the object str…
Light_handle
  • 3,947
  • 7
  • 29
  • 25
234
votes
76 answers

What's the best name for a non-mutating "add" method on an immutable collection?

Sorry for the waffly title - if I could come up with a concise title, I wouldn't have to ask the question. Suppose I have an immutable list type. It has an operation Foo(x) which returns a new immutable list with the specified argument as an extra…
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
229
votes
19 answers

String is immutable. What exactly is the meaning?

I wrote the following code on immutable Strings. public class ImmutableStrings { public static void main(String[] args) { testmethod(); } private static void testmethod() { String a = "a"; System.out.println("a…
Anuj Balan
  • 7,629
  • 23
  • 58
  • 92
217
votes
13 answers

What would a "frozen dict" be?

A frozen set is a frozenset. A frozen list could be a tuple. What would a frozen dict be? An immutable, hashable dict. I guess it could be something like collections.namedtuple, but that is more like a frozen-keys dict (a half-frozen dict).…
dugres
  • 12,613
  • 8
  • 46
  • 51
211
votes
11 answers

Immutable vs Unmodifiable collection

From the Collections Framework Overview: Collections that do not support modification operations (such as add, remove and clear) are referred to as unmodifiable. Collections that are not unmodifiable are modifiable. Collections that additionally…
Cratylus
  • 52,998
  • 69
  • 209
  • 339
1
2 3
99 100