[13]immutability in python – fundamentals

Rule: immutability is on objects, not on variables. Actually, same as other languages.

Rule: An object’s mutability is determined by its type; for instance, numbers, strings and tuples are immutable, while dictionaries and lists are mutable.

Examples of containers are tuples, lists and dictionaries. The value of an immutable container object (like a tuple instance) that contains a reference to a mutable object can change when the latter’s value is changed; however the container is still considered immutable, because the collection of objects it contains cannot be changed. So, immutability is not strictly the same as having an unchangeable content, it is more subtle. See http://docs.python.org/2/reference/datamodel.html

In most cases, when we talk about the value of a container, we imply the values, not the identities of the contained objects; however, when we talk about the mutability of a container, only the identities of the immediately contained objects are implied.

I think for a custom MyClass that HAS-A object field myField, MyClass immutability means the content of myField is unchangeable. That’s the java concept of immutability.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s