Python3: Mutable, Immutable… everything is an object!
Introduction.
Python is an object oriented programming language. Unlike procedure oriented programming, where the main emphasis is on functions, object oriented programming stress on objects.
Object is simply a collection of data (variables) and methods (functions) that act on those data. And, class is a blueprint for the object.
We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object.
As, many houses can be made from a description, we can create many objects from a class. An object is also called an instance of a class and the process of creating this object is called instantiation.
Id() funtion
id() is an inbuilt function in Python.
Syntax:
id(object)
As we can see the function accepts a single parameter and is used to return the identity of an object. This identity has to be unique and constant for this object during the lifetime. Two objects with non-overlapping lifetimes may have the same id() value. If we relate this to C, then they are actually the memory address, here in Python it is the unique id. This function is generally used internally in Python.
Examples:
Input : id(1025)
Output : 140365829447504
Output varies with different runsInput : id("geek")
Output : 139793848214784
type() function
type() method returns class type of the argument(object) passed as parameter. type() function is mostly used for debugging purposes.
Two different types of arguments can be passed to type() function, single and three argument. If single argument type(obj) is passed, it returns the type of given object. If three arguments type (name, bases, dict) is passed, it returns a new type object.
Syntax :
type(object)
type(name, bases, dict)
Mutability and Immutability.
So far each time we study a type of variables we indicate whether they are mutable or immutable.
When a variable is of an immutable type, such as a string, it is possible to assign a new value to that variable, but its content cannot be modified.
>>> a="example"
>>> a="other"
>>> a[2]="c"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
This is because when a new assignment is made, the string itself is not modified, but the variable a goes to point to another chain. On the other hand, it is not possible to assign a new character in a position, as this would imply modifying the immutable string.
In the case of the mutable parameters, the assignment has the same behavior, that is to say that the variables begin to point to a new value.
>>> list1 = [10, 20, 30]
>>> list2 = list1
>>> list1 = [3, 5, 7]
>>> list1
[3, 5, 7]
>>> list2
[10, 20, 30]
Something important to keep in mind in the case of the variables of the mutable type is that if there are two or more variables that point to the same data, and this data is modified, the change is reflected in both variables.
>>> list1=[1, 2, 3]
>>> list2 = list1
>>> list2[1] = 5
>>> list1
[1, 5, 3]
Why does it matter and how differently does Python treat mutable and immutable objects.
In python everything is interpreted as object. All objects can be either classified as Mutable or immutable. Mutable object can be change able after creation and immutable objects can’t be change able once created.
Common immutable type:
numbers: int(), float(),complex()
Immutable sequences: str(), tuple(), bytes()
Common mutable type (almost everything else):
mutable sequences: list(),bytearray()
set type: set()
type of mapping: dict()
Classes, class instances.
A trick to quickly test whether a type is mutable or not, is to use the built-in function id().
Examples, using in full,
>>> i = 1 >>> id(i) ***704 >>> i += 1 >>> i 2 >>> id(i) ***736 (different from ***704)
Using in the list,
>>> a = [1] >>> id(a) ***416 >>> a.append(2) >>> a [1, 2] >>> id(a) ***416 (same with the above id)
Whether an object is mutable or not depends on its type. This does not depend on whether or not you have certain methods, or on the structure of the class hierarchy.
User-defined types (that is, classes) are generally mutable. There are some exceptions, such as simple subclasses of an immutable type. Other types include some types immutable incorporated as int, float, tuple nd str as well as some kinds of Python implemented in C.