Does Python have an immutable list?

Yes. It’s called a tuple. So, instead of [1,2] which is a list and which can be mutated, (1,2) is a tuple and cannot. Further Information: A one-element tuple cannot be instantiated by writing (1), instead, you need to write (1,). This is because the interpreter has various other uses for parentheses. You can also do away with parentheses altogether: 1,2 is the same as (1,2) Note that a tuple is … Read more

String is immutable. What exactly is the meaning?

Before proceeding further with the fuss of immutability, let’s just take a look into the String class and its functionality a little before coming to any conclusion. This is how String works: This, as usual, creates a string containing “knowledge” and assigns it a reference str. Simple enough? Lets perform some more functions: Lets see how the below statement works: This appends a … Read more

Remove specific characters from a string in Python

Strings in Python are immutable (can’t be changed). Because of this, the effect of line.replace(…) is just to create a new string, rather than changing the old one. You need to rebind (assign) it to line in order to have that variable take the new value, with those characters removed. Also, the way you are doing it is going to be kind of … Read more

Immutable class?

What is an immutable object? An immutable object is one that will not change state after it is instantiated. How to make an object immutable? In general, an immutable object can be made by defining a class which does not have any of its members exposed, and does not have any setters. The following class … Read more

Error: “Cannot modify the return value” c#

This is because Point is a value type (struct). Because of this, when you access the Origin property you’re accessing a copy of the value held by the class, not the value itself as you would with a reference type (class), so if you set the X property on it then you’re setting the property on the copy and then discarding it, leaving … Read more