Sitemap
Godsfavour Chukwudi

Godsfavour Chukwudi

Python Objects Explained

4 min readJul 28, 2023

--

Python, a versatile and sophisticated programming language, provides developers with a unique feature set that allows them to deal with mutable and immutable objects. This distinction is critical for understanding how Python handles data and how it affects the language's many characteristics. This blog article will examine the differences between mutable and immutable objects and how Python treats them.

Let us begin by introducing the id and type. Each object in Python has a unique identification known as an id that remains consistent throughout its existence. The id serves as a reference to the object's memory location and may be acquired using the built-in id() method. Also, objects are classified into types describing their behaviour and related operations. The type() method is used to determine the type of an object. Let us use a simple example to demonstrate this:

x = 42: This line gives the variable x the value 42. Variables in Python are not explicitly defined with a data type; instead, they dynamically take on the type of the value to which they are allocated. x is now an integer variable with the value 42.

print("ID:", id(x)): The id() method in Python produces an object's unique identifier (memory address). In this line, we use id(x) to obtain the memory location of the integer object with the value 42, which is stored in the variable x. The result is then shown with the text "ID:" using the print() method.

type(x): print(“Type:”): The type() method returns an object’s data type. In this case, we use type(x) to ascertain the data type of the integer value stored in the variable x. The print() method is used again to display the result alongside the word "Type:".

Mutable objects are objects that can be changed after they are created. Lists, dictionaries, and sets are examples of mutable objects in Python. When you update the value of a mutable object, its ID does not change. This is because the update does not affect the object's memory location. Let's look at an example:

List1 is a list in the given code, and we use the append() function to change it by appending element 4 to the end of the list.

Immutable things, on the other hand, cannot be modified after they are created. In Python, immutable objects include integers, strings, and tuples. When you update an immutable object, a new object with a different ID is produced, while the original object remains unaffected. Let's put this into practice:

Press enter or click to view image in full size

In this case, x is a string, an immutable object in Python. Initially, both x and y relate to the string "Hello". When we add the text "World" to x (x += "World"), a new string "Hello World" is produced, and x now refers to this new object. Because strings cannot be modified, the original string "Hello" stays untouched, and y still refers to it. The output shows that changing x does not affect the value of y, demonstrating the immutability of strings in Python.

Python treats mutable and immutable objects differently, and this matters. The distinction between mutable and immutable objects is critical because it substantially influences how Python handles certain operations, memory management, and function arguments. When mutable objects are sent as function parameters, all changes made to the object within the function are also reflected outside the function. On the other hand, immutable objects cannot be altered within a function, maintaining data integrity and avoiding undesired side effects. Consider this illustration:

Press enter or click to view image in full size

The list (a mutable object) was updated inside the method and mirrored outside it in this example. However, after sending it to the function, the integer (an immutable object) stayed untouched.

Understanding the difference between mutable and immutable Python objects is critical for designing efficient, reliable, and maintainable code. The way Python handles these objects maintains data integrity and avoids unexpected behaviour. Recognising the consequences of mutability as developers may help us make informed design decisions and optimise performance in our Python programmes.

Get Godsfavour Chukwudi’s stories in your inbox

Join Medium for free to get updates from this writer.

Python, R. (2023, June 16). Python's Mutable vs Immutable Types: What's the Difference? realpython.com. https://realpython.com/python-mutable-vs-immutable-types/#:~:text=Mutable%20objects%20are%20those%20that,same%20type%20with%20different%20values.

Foundation: The Objective-C Standard Library | 4.1 General Concepts | InformIT. https://www.informit.com/articles/article.aspx?p=1436920&seqNum=2

O. (n.d.). Mutable | Oz.io. https://oz.io/Mutable

id function in Python. (2023, June 4). GeeksforGeeks. https://www.geeksforgeeks.org/id-function-python/

Chelliah, I. (2021, December 15). Object Value vs Object Type vs Object Id in Python — Analytics Vidhya — Medium. Medium. https://medium.com/analytics-vidhya/object-value-vs-object-type-vs-object-id-in-python-aafd0444d23c

--

--