Python3: Mutable, Immutable... everything is object!
In Python no matter the case, everything has a series of attributes(built-in) that can be accessed and modified, we call this "object" and all objects in Python can be either mutable or immutable.
When an object is initiated, it is assigned a unique object id. Its type is defined at runtime and once set can never change, however its state can be changed if it is mutable. Simple put, a mutable object can be changed after it is created, and an immutable object can’t.
ID and TYPE
"Id" of an object is the unique address in memory and some variables of type "int" have a fixed id to obtain the "id" of a variable just execute this function 'id (object)'.
It helps to define the type of class or variable of the object that is passed as an argument and returns the type of the object (int, float, str, etc ...) and is very useful for debugging.
You can get the data type of any object by using the type () function.... 'type (object)'.
When we declare a variable, a variable is created in memory at a memory address. If then next we assign (the variable is a cursor or pointer that will "point to the memory address of its value") a mutable value and it will be created in another memory location; then the above variable will point to the mutable object that contains its value.
Depending on the type of data that we send to the function, we can differentiate behaviors:
Pass by value: A local copy of the variable is created inside the function.
Pass by reference: The variable is handled directly, the changes made inside the function will also affect it outside.
When you call a function in Python and give it some arguments... Are they passed by value? Reference? No! They're passed by assignment.
The object passed as an argument is bound to the parameter variable as well. If you reassign the parameter variable to a new object, that new object is created and bound to the parameter name.
That name is no longer bound to the object it previously had. However, note that the variable used as the argument to the function is still bound to that original object.
Recommended by LinkedIn
Integer pre-allocation
Uses a PyIntObject type array of 262 pointers to store the small integers falling in the range from -5 to 256.
static PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];
It means that the small integers will not use the group of objects. Instead, they will use the list of pointers given above. Because, the small integers will end up using the same PyIntObject integer object but with a different reference count.
Or the big integers get their allocation from the integer object pool. And each of them would have its own PyIntObject type object.
Tuples vs frozensets in Python
Tuples:
Frozensets:
Will be very useful for you to understand how python objects are stored in memory and how to access them, also when you are going to create complex objects such as dictionaries and what happens when accessing their specific values.