Reputation: 99526
What are the purposes of introducing immutable objects in Python?
Does it have something to do with Python using reference model (i.e. variables are pointers to objects instead of containers of objects' values) ?
I guess not, because Python uses reference model for all types, and not all the types require immutable objects.
Is immutability in Python used as part of functional paradigm? If yes, how?
For example, we can’t change a string by assigning to one of its positions.
>>> S
'Spam'
>>> S[0] = 'z' # Immutable objects cannot be changed
...error text omitted...
TypeError: 'str' object does not support item assignment
But we can always build a new one and assign it to the same name. Does that betrays the purpose of immutability in functional paradigm, and aligns with imperative paradigm? But in imperative paradigm, implementing mutability in terms of immutability and reference model is inefficient .
>>> S = 'z' + S[1:] # But we can run expressions to make new objects
>>> S
'zpam'
What are the reason that some types are designed to use immutable objects (numbers, strings, tuples), while other types (lists, dictionaries, sets) use mutable objects, instead of all the types immutable or mutable?
Is there some thumb of rule for telling if a type requires immutability or mutability? Are primitive objects (i.e. as opposed to composite objects) necessary immutable?
Thanks.
Upvotes: 1
Views: 630
Reputation: 12191
What are the purposes of introducing immutable objects in Python?
What's the point of introducing immutable objects in any language?
...we can’t change a string by assigning to one of its positions... But we can always build a new one and assign it to the same name. Does that betrays the purpose of immutability in functional paradigm, and aligns with imperative paradigm? But in imperative paradigm, implementing mutability in terms of immutability and reference model is inefficient.
This isn't all that uncommon of an implementation, actually - C# strings are implemented in exactly the same way (mutable reference to an immutable object).
Some languages (like C) that used character arrays for strings had de facto immutable string lengths (but each character was mutable).
Keep in mind that there are numerous competing definitions of "immutability." (I read a paper awhile back that listed and explained all of them, but I can't locate it right now). For example, does it mean a constant reference to a mutable object (shallow immutability)? Or a constant reference to an immutable object (deep immutability)? Or even a "non-fixed" reference to an immutable object? It really depends on how "deep" you want to make the immutability and how you can implement it without "breaking" backwards compatibility. (On the backwards compatibility issue, note that C++, C#, Java, F#, and many other languages actually had to introduce special keywords for constant variables - e.g. final
in Java, let
in F#, readonly
in C#).
One other point: many languages (C#, Python) have functional features without necessarily being purely (or even predominantly) functional languages per se. A lot of times (e.g. in C#), the functional features are technically newer additions to the language, so some functional features may not be implemented in as "pure" of a functional style as you'd like in order to maintain backwards compatibility.
Also, part of the consequence of allowing for multiple paradigms is that you can typically "mix" them in ways that aren't always entirely healthy - it's typically difficult or impossible to "ban" paradigm-mixing from a syntactical point of view.
What are the reason that some types are designed to use immutable objects (numbers, strings, tuples), while other types (lists, dictionaries, sets) use mutable objects, instead of all the types immutable or mutable?
Because Python is a multi-paradigm language. Immutable data types are suitable for functional programming, but usually you want mutable data types for object-oriented programming. Thus, the language has to have features that are appropriate for both paradigms. As the official documentation on Python's functional features states,
The designers of some computer languages choose to emphasize one particular approach to programming. This often makes it difficult to write programs that use a different approach. Other languages are multi-paradigm languages that support several different approaches. Lisp, C++, and Python are multi-paradigm; you can write programs or libraries that are largely procedural, object-oriented, or functional in all of these languages. In a large program, different sections might be written using different approaches; the GUI might be object-oriented while the processing logic is procedural or functional, for example.
So, basically, Python wants to allow you to choose your paradigm (or even be able to mix them within the same program). This can actually be quite convenient - many frameworks (e.g. .NET) are now encouraging it. (WPF/XAML, for example, tends to encourage a declarative paradigm for GUIs).
Upvotes: 2