Everything is object
INTRODUCTION
You probably know the following phrase "Everything in Python is an object". A thing? What is an object? We will define an object as a data abstraction. Python provides a variety of data types that can be handled as objects. Among them, we can find that strings, lists, functions and even modules are all objects. Now let’s see why everything is called an object in Python. Python has a quirk and treats its data type as mutable or immutable, which means that if the value can be changed, the object is called mutable, and if the value cannot be changed, the object is called immutable
ID :
The id() function receives an object as a parameter and returns another object, which serves as a unique identifier for the first object. The return value is an integer indicating the memory address of the storage object.
Sintax: id (object)
As mentioned above, python deals with mutable and immutable data types, and now we will better understand what the previous two expressions refer to.
Immutable objects
The type is immutable, meaning that it cannot be changed after it is created.
Immutable types
1. Numbers
2. String
3. Tuples
Let's look at an example
In this example, we have assigned the string "MAHDI" to the variable name. Then, we tried to change the second letter of the string, which gave us an error.
The error message is as follows: "The'str' object does not support item assignment"
This happens because we are trying to change the value of an immutable object, because strings in Python are immutable.
What we can do is to assign a new value to the variable, which is different from the changed string
It seems that we are changing the string, but in fact we are assigning a new value to it, because if you realize it, we will pass the = sign and the new value we want the variable to have.
Then you can use immutability to ensure that an object remains the same throughout the program
Mutable objects
Mutable object values can be changed anytime and anywhere after creation.
Mutable types
1.Lists
2.Sets
3.Dictionaries.
Let's look at an example
In this example, we assigned the characters 'M', 'A', 'H', 'D', 'I' to the list name. Then, we tried to change the second letter in the list, and this operation was successfully completed. This happens because we are trying to change the value of mutable objects, because lists in Python are mutable.
Why is it important, and how does Python handle mutable and immutable objects?
Python handles mutable and immutable objects in different ways. Access to immutable objects is faster than access to mutable objects. Similarly, immutable objects are fundamentally expensive to "change" because doing so involves creating copies. It is cheap to change objects that are mutable.
When you need to adjust the size of an object, a mutable object is great, for example, it will be dynamically modified
How to pass parameters to functions, and what does this mean for mutable and immutable objects?
Python Immutable Function Arguments
Immutable objects in python (such as numbers, strings, or tuples) are passed as parameters in the function, and they change within the function block, so their behavior is similar to a copy of the object. A new local copy of the calling object will be created and manipulated within the scope of the function block. The calling object will remain unchanged. Therefore, the calling block does not notice any changes made to immutable objects within the scope of the function block.
This value is only modified in the function block, outside the function, its value remains unchanged.
Python Mutable Function Arguments
If one of these parameters is modified in the body of the function to point to another value, the change will not be modified outside the function. On the other hand, if the content of any variable parameter is modified, the change will be modified outside the function.
Now that you know how Python handles data, it’s time to program and put what you learned into practice.