DEV Community

Best Verie Iradukunda
Best Verie Iradukunda

Posted on

1

Exploring Python: Everything is an Object

ID and Type:
In Python, every object has an identity (id) and a type. The id uniquely identifies each object, acting as its address in memory. This means that even basic data types like integers, strings, and lists are treated as objects in Python.

Example of getting ID and type of an object
x = 42
print("ID:", id(x))
print("Type:", type(x))

Enter fullscreen mode Exit fullscreen mode
Results

ID: 140736591034160
Type: <class 'int'>

Mutable Objects:
Some objects in Python are mutable, meaning they can be modified after creation. For instance, lists and dictionaries can have their elements or key-value pairs changed, added, or removed without changing their identity.

Example of modifying a mutable object
my_list = [1, 2, 3]
my_list.append(4)
print(my_list)  # Output: [1, 2, 3, 4]

Enter fullscreen mode Exit fullscreen mode

Immutable Objects:
On the other hand, immutable objects cannot be modified once they are created. Examples of immutable objects in Python include integers, strings, and tuples. Any attempt to change an immutable object results in the creation of a new object.

Example of trying to modify an immutable object
my_string = "Hello"
my_string += " World"
print(my_string)  # Output: Hello World

Enter fullscreen mode Exit fullscreen mode

Why Does it Matter and How Python Treats Mutable and Immutable Objects Differently:
Understanding the difference between mutable and immutable objects is crucial for writing efficient and bug-free Python code. Python treats mutable and immutable objects differently in terms of memory management and behaviour. Immutable objects ensure data integrity, while mutable objects offer flexibility but require careful handling to avoid unexpected changes.

How Arguments are Passed to Functions and Implications for Mutable and Immutable Objects:
In Python, arguments are passed to functions by object reference. This means that when you pass an object to a function, you are passing a reference to the object rather than the object itself. For immutable objects, this is similar to passing by value, as the function cannot modify the original object. However, for mutable objects, changes made within the function can affect the original object outside the function, leading to unexpected behaviour if not handled correctly.

Passing Immutable Objects:
def modify_immutable(x):
    x += 1
    print("Inside the function:", x)

num = 10
modify_immutable(num)
print("Outside the function:", num)

Enter fullscreen mode Exit fullscreen mode
Output

Inside the function: 11
Outside the function: 10

Passing mutable Objects:
def modify_mutable(lst):
    lst.append(4)
    print("Inside the function:", lst)

my_list = [1, 2, 3]
modify_mutable(my_list)
print("Outside the function:", my_list)

Enter fullscreen mode Exit fullscreen mode
Output

Inside the function: [1, 2, 3, 4]
Outside the function: [1, 2, 3, 4]

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

đź‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay