Everything is an object in Python!
Laura Callejas
Bilingual Web Developer | Front End Developer | JavaScript | HTML5 | CSS3 | Figma | Sass | React
Python is an object-oriented programming language, and in Python everything is an object, an?object?is an entity that contains data along with associated metadata and/or functionality.
Every object has these three attributes:
In order to know the address of the objects we have to use the function id().
The?id()?function returns a unique id for the specified object.
All objects in Python has its own unique id.
The id is assigned to the object when it is created.
The id is the object's memory address, and will be different for each time you run the program. (except for some object that has a constant unique id, like integers from -5 to 256)
Example:
print("Id of 10 is: ", id(10)
print("Id of 10.5 is: ", id(10.5)))
------------------------------------
output :
Id of 10 is: 879111306
Id of 10.5 is: 35217761
If you need to know the type of a specific variable use?type()?function returns the type of the specified object.
Example:
str = 'Python
numbers = [1,2,3,4]
numbers_dict = {'one':1,'two':2,'three':3}
print(type(nums))
print(type(lang))
print(type(nums_dict))'
----------------------------------------------------
output:
<class 'str'> <br>
<class 'list'><br>
<class 'dict'>
Object mutability is one of the characteristics that makes Python a dynamically typed language. Though Mutable and Immutable in?Python?is a very basic concept, it can at times be a little confusing due to the intransitive nature of immutability.
What is Mutable?
Mutable is when something is changeable or has the ability to change. In Python, ‘mutable’ is the ability of objects to change their values. These are often the objects that store a collection of data.
For example:
As we can see on the example code, once mylist is created has an initial address number, after we change the value of the list by adding one more item("black") on mylist this address number is the same, so by this exercise we can see that we can change the value but the memory address has didn't change, This confirms that we did not create a new object, rather, the same object was changed or mutated.
What is Immutable?
Immutable is the when no change is possible over time. In Python, if the value of an object cannot be changed over time, then it is known as immutable. Once created, the value of these objects is permanent.
For example:
领英推荐
As we can see on the example of the previous code we can not change the value of the tuple once it's created , in the case that we want to add , pop or even change an item of the tuple we have to create a new one in order to do it, and it will be the same with the other data type mentioned previously, we can not modify the value again.
ARGUMENTS PASSED BY VALUE.
Lest's see an example :
we have a function with two arguments
def function(a, b):
a?=?"hello3"
b[0]?=?10
a is a ,string with value "hello3"
b is a list, with value 10
This function just assigns value to a couple variables
Now we call the function with other variables
str?=?"how?are?ya?13" #immutable
list?=?[5] #mutable
function(str , list)
print(int)
print(list)
---------------------------
output:
how?are?ya?13
[10]
What happened here? Why the value of the str is the same?
couple concepts you need to understand before we continue:
By definition,?pass by value?means you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter. Use pass by value when when you are only "using" the parameter for some computation, not changing it for the client program.
In?pass by reference?(also called pass by address), a copy of the address of the actual parameter is stored. Use pass by reference when you are changing the parameter passed in by the client program.
Well string as we mention before they are inmutables (Immutable types are passed by value, then the function accesses a copy and not the original value), that's what we can not change the value onces the string is create it, but in the other hand list has a new value again because is a mutable object (Mutable types are passed by reference).
Thanks to read me , I hope this information were helpful for you !