Everything is a object on python
Python3: Mutable, Immutable… everything is object!
Hello and welcome to another blog in this case i’m going to talk about what i learned and why everything is a object in python.
Objects in python follows the principle of “First-Class Everything”. This concept centers around the idea of having every single item of data in python belonging to a class as an object with attributes and methods.
id and type: Each object has an ID and a type, what identifies them.
id: id is a built-in function and is used by passing the object as an id(object) parameter to tell us what is the id of an object
type: type is another built-in function too, and is used to know what type a certain object is, it’s syntax is the same, you must pass the object
Mutable and inmutable
Now that we know that all the objects have and id and a type, it’s time to know that there are mutable and inmutable objects.
The inmutable objects are those that can’t be modified meanwhile the mutable ones are all those that can be changed.
For example, in this case, “hello world” is a single object referred to by two different variables.
>>> text = "hello world"
>>> another_text = "hello world"
>>> id(text) 139774005161352 >>> id(another_text) 139774005161352
How objects are passed on to functions
Now that we know that we have mutable and immutable objects, the way we pass them on to functions is important.
The memory efficiency is greatly affected when the right objects are used.
If a mutable object is called by a reference in a function, it can change the original variable itself. To avoid this, the original variable needs to be storage in another variable. Immovable objects can be called by reference because their value cannot be changed anyway.
And that’s all for this blog, thanks for reading and #happylearning