DEV Community

Denyse
Denyse

Posted on

Exploring Python Objects: A Dive into Identity, Types, and Mutability

Sup reader!!,

Introduction

Python, a dynamic and robust programming language, it opens the door to the captivating realm of objects. In this article, we are going to explore the fundamental concepts of identity, types, and mutability within Python's object-oriented paradigm. Mastery of these principles is essential for crafting optimized and error-free code.

Let's get started

What's id in Python?

In Python, the built-in function id() is a function that retrieves the unique integer representing an object's memory location. Every object in Python has a distinct identity, even if they share the same value.

What's type in Python?

In Python, the type() function is a function that reveals an object's class or type, aiding informed decisions on manipulation or interaction.

Mutable vs. Immutable Objects

In Python, mutable objects are those whose state or content can be changed after they are created whereas immutable objects are those objects that are essentially read-only any attempt to modify their content results in the creation of a
new object.

Here are the examples of mutable and immutable objects:

Mutable Objects:

  • Byte array
  • Dictionary
  • Set
  • List

Immutable Objects:

  • Numbers (int, float, complex)
  • String
  • Tuple
  • Bytes
  • Frozen Set

Understanding mutability is vital, as it impacts how objects behave during assignments and referencing.

Variable Assignment vs. Referencing in Python:

Assigning a value to a variable establishes a reference to the object. In Python, variables are names bound to objects, making it vital to distinguish between assignment and referencing to prevent unforeseen outcomes in your code.

Memory Storage of Immutable Objects:

Immutable objects, once initialized, remain unchangeable. A new object with the updated value is created if modifications are attempted. This approach preserves the integrity of the original object's value.

Examples with Memory Schema

Let's explore examples to understand the concepts we discussed earlier clearly.

Example 1: Immutable Object

name = "Alice"

name[0] = "B"  # Throws an error as strings are immutable.

Enter fullscreen mode Exit fullscreen mode

In this example, trying to modify the first character of the string name results in an error, highlighting the immutability of strings.

Example 2: Mutable Object

colors = ["red", "green", "blue"]

colors[0] = "yellow"

Enter fullscreen mode Exit fullscreen mode

Here, the first element of the list colors is successfully changed to "yellow", highlighting the mutability of lists.

Variable Value Management in Function Calls

When passing a variable to a function in Python, the mechanism used is known as "pass by assignment." This implies that the function receives a reference to the object, rather than a copy. It's essential to learn this mechanism for anticipating your function's behavior.

If you've read this far,

Image description

Top comments (0)