Python ID & Objects

Ever wandered what happens under the hood in a python application? We will be covering the very basics (elementary) concepts in Python, and in Object Oriented Programming (OOP for short). If you are new to python, this article is for you. We are covering three main things:

ID
Mutable Objects
Immutable Objects.

Grab your favorite beverage, and let’s dive in!

Why Does it Matter and Python’s Treatment

The distinction between mutable and immutable objects is vital because Python treats them differently. While changes to mutable objects affect their references elsewhere in the code, immutable objects guarantee data integrity.

Recognizing this distinction enables developers to choose the appropriate data structures for their specific needs, balancing flexibility and safety.

Function Arguments and Implications

Understanding how Python handles function arguments is paramount. In Python, function arguments are passed by assignment, but the nuances become apparent when dealing with mutable and immutable objects.

Mutable objects can be modified in place within a function, affecting the original object, while immutable objects remain unchanged outside the function. This behavior shapes the design and behavior of Python functions.

ID and Type

Every Python object has a unique identity (ID) and a type. ID: represents the object’s location in memory type: defines the category to which the object belongs. This dual aspect of identity and type forms the foundation of Python’s object model.

Alright, imagine each variable in Python has its own ID card and personality. The ID is like a unique address, and the type is the kind of character it is.

#Print the ID

my_variable = 42
print(f”ID of ‘my_variable’: {id(my_variable)}”)

# Print the Type
print(f”Type of ‘my_variable’: {type(my_variable).__name__}”)
my_variable = “Hello, Python!”
print(f”My ID: {id(my_variable)}”)
print(f”My Type: {type(my_variable).__name__}”)ID and Type:
Mutable Objects

Understanding mutability is fundamental, as it influences how objects are modified and shared between different parts of a program. Mutable objects in Python, such as lists, can be altered after creation.

Modifying these objects directly impacts their content, making them susceptible to in-place changes. In other words, mutable objects are like superheroes with the ability to change over time.

Lists: Ordered and mutable sequences of elements.

my_list = [1, 2, 3]
Dictionaries: Unordered collections of key-value pairs.

my_dict = {‘key1’: ‘value1’, ‘key2’: ‘value2’}
Arrays (from the array module): Typed arrays that can be modified.

from array import array
my_array = array(‘i’, [1, 2, 3]) # ‘i’ denotes integer type
Sets: Unordered collections of unique elements.

my_set = {1, 2, 3}
Byte Arrays: Mutable sequences of bytes.

my_bytearray = bytearray(b’hello’)
These objects can be modified after creation, making them mutable. Keep in mind that changes to mutable objects can impact their references elsewhere in the code.

Immutable Objects

On the flip side, immutable objects like tuples and strings cannot be changed once created. This immutability has implications for memory efficiency and safety, as it ensures the integrity of data without the risk of unintended modifications.

Tuples are the rockstars here.

Once created, they stay as they are, and that’s pretty cool.

#Tuple Example

my_tuple = (1, 2, 3)
print(f”Original Tuple: {my_tuple}”)
Examples of Tuples in Real World Applications

Here are different examples showcasing the usage of tuples and strings as immutable objects in Python:

#Coordinates

coordinates = (3, 5)
#Days of the Week

days_of_week = (‘Monday’, ‘Tuesday’, ‘Wednesday’, ‘Thursday’, ‘Friday’, ‘Saturday’, ‘Sunday’)
#RGB Color

rgb_color = (255, 0, 128)
#Student Life

student_info = (‘John Doe’, 21, ‘Computer Science’)
#Greeting

greeting = “Hello, World!”
#DNA Sequence

dna_sequence = “ATCGGCA”
#File Path

file_path = “/path/to/file.txt”
#Message

message_template = “Hi {name}, your account balance is {balance}.”
In Conclusion

Delving into the intricacies of Python’s object model, understanding mutability, and recognizing the impact on function arguments empower developers to write more robust and efficient code. By navigating the dynamic interplay between mutable and immutable objects, Python enthusiasts can harness the language’s power more effectively in their projects.