Objects in Python

In this blog, we will discuss about objects in Python. Everything in Python is an object. We will take one example and will understand concept of objects in python.

Python Object

As already said, anything that we use or include in python code or program is an object.
What really happens when we type an instruction like lang = ‘English’ in python.
-In this case, object is created.
-This object gets an ID and type is set to string and the value to ‘English’.
-A name lang is placed in global namespace and points to that object.
-We can retrieve value of that object by simply using name lang which is pointing to that object.
In the below code, variable lang is created and assigned a value ‘English’. This statement will create an object and will have its ID. ID is displayed using built-in id() function. Use name lang to print the value of that object.

lang ='English'



Mutable and Immutable objects in Python

It is very important to understand difference between mutable and immutable objects because it can affect python code
Mutable Object
-Value of objects can be change
-Value of immutable objects can not be change


In the below code, variable count is created with a value of 10. This code will create an object and have ID and type is set to int (integer Number).

count = 10

Print statement displays ID by using id() function. Getting ID value 8790903687104

count = 10



Now what would happen if we try to change value of count variable. In this case, current object does not update its value to 20 but a new object is created and will have new ID with a value of 20. Variable count will point to only object at a time. It means that we haven’t change the value of an object but instead created a new one with new ID and value and count currently pointing to new object

count = 10
count = 20



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s