Python - Encapsulation

25.
What are some common examples of encapsulation in popular Python libraries?

Common Example of Encapsulation in Python Libraries:

The datetime module in Python provides encapsulation by hiding the internal details of date and time representation. Users can access and manipulate date and time objects through well-defined interfaces. Let's explore an example:

from datetime import datetime

# Creating a datetime object
current_datetime = datetime.now()

# Accessing encapsulated attributes through methods
year = current_datetime.year
month = current_datetime.month
day = current_datetime.day

# Displaying the details
print(f"Year: {year}")
print(f"Month: {month}")
print(f"Day: {day}")

In this example, the datetime module encapsulates the details of date and time representation. Users can access the current date and time using the datetime.now() function, and then access specific attributes (year, month, and day) through well-defined interfaces. The internal implementation details are hidden, providing encapsulation.

Output(sample):

Year: 2024
Month: 2
Day: 3