-1

The main ideas of immutability are kept the same throughout the scope of OOP and functional programming or do, for example, Java and Python have their own versions of immutability. More specifically do the following hold in all languages?

  • Mutable Objects: Set, Dict, List
  • Immutable Objects: Bool, Int, Float, String, Tuple
  • In python two immutable objects with the same value also have the same id, two references one value.
  • In python again two mutable objects with the same value don't share the same id, two references two values.
  • Does this idea of two references binding together in mutable objects hold in all languages? And the reverse as well, that is, bindings cannot be changed, meaning that references can only change the value they are pointing to.

    i = {1,2,3} # Set, a mutable object
    j = {1,2,3}
    i is j
    False
    i = j
    j.remove(3)
    i is j
    True
    

I'm asking because for example in scripting languages objects are passed by references (in other languages passed by value or in C where we have both) so doesn't this change the whole notion of immutability?

Ari
  • 324
  • 3
  • 13
  • 1
    There are plenty of languages that don't even have all the concepts you listed. No mutable objects, or no immutable objects, or none of those data structures, or multiple different implementations of those data structures... your perspective seems to be very shaped by Python. – user2357112 Sep 23 '18 at 01:30
  • 1
    You are asking a number of questions for *all* programming languages. This makes too broad to be answered completely on SO. Please reduce the scope of the question(s) to make it answerable. – Klaus D. Sep 23 '18 at 01:45
  • 1
    Python isn't really a pass ny reference language. Please see https://stackoverflow.com/questions/10262920/understanding-pythons-call-by-object-style-of-passing-function-arguments and the links there. – PM 2Ring Sep 23 '18 at 02:09
  • All variables in Python are not really variable like in C, think of them are pointers. In your example,` {1, 2, 3}` create a new set object, then assign it to a label named `i`. Another `{1, 2, 3`} create another new set object, then assign it to a label named `j`. The line `i = j` copy the pointer value from `j` to `i`, so `j` is equal to `i` now. You can think the `id()` is the memory location of the object. – hevangel Nov 13 '19 at 01:25

2 Answers2

1

What is my understanding of the difference between mutable vs immutable in python is the that the first can be changed by indexing. For example, the following x list can be changed by indexing!

x = [1,2,3]
x[0] = 10

y = (1,2,3)
y[0] = 10 # this will raise an error. tuple is not mutable. 

y = x 

id(y) == id(x) #gives true. since y is a reference to x
y[0] = 10
print(y)
[10, 2, 3]
print(x)
[10, 2, 3] # x is changed as well! y and x are same same.

every time you create lists or sets or tuples with unique names even though they contain the same dataset, still they are not the same list mapped into the memory. each has its unique id.

Khalil Al Hooti
  • 4,207
  • 5
  • 23
  • 40
1

If you have any object, even literal ones, it needs to use some space in memory.

This memory needs to be mutated by the language runtime and it is the same if it's immutable or not. Thus mutable objects mutate memory when object gets created.

Thus an immutable object is one that either is ensured not to be changed at compile time or protected by the runtime when the program runs.

In python two immutable objects with the same value also have the same id, two references one value.

I don't think this is guaranteed at all. Eg.

x = (1,2,3)
y = (1,2,3)
x is y
// => False

When I run it in my repl. If it's anything like Common Lisp and Java it might happen that implementations are free to reuse memory locations of the same literals and thus any boolean result would be acceptable.

Sylwester
  • 47,942
  • 4
  • 47
  • 79