E.RafatiNasr
E.RafatiNasr

Reputation: 1

Why is there contradiction in being mutable and immutable variable and string?

As it is said that the data in a variable is changeable in a program, on the other hand, in data structure the strings, for example, are immutable. So here it is a contradiction between variable and data structure. Can anyone help me in solving this matter?

Upvotes: 0

Views: 101

Answers (2)

juanpa.arrivillaga
juanpa.arrivillaga

Reputation: 95917

There is no contradiction. A variable isn't the value. To say that an object is mutable means that it exposes mutator methods. Variables are mutable when they can be reassigned.

Upvotes: 0

Bikramjeet Singh
Bikramjeet Singh

Reputation: 686

>>> name = 'E.RafatiNasr' # your name assigned to a 'name' variable
>>> name[0] = 'A' # I tried changing 'E' in your name to 'A'

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment

Oops! What happened?

Upvotes: 1

Related Questions