What is encapsulation in Python?

96 views

What is encapsulation in Python?

What is encapsulation in Python?

solveurit24@gmail.com Changed status to publish February 13, 2025
0

Encapsulation is the practice of hiding data and methods within a class, controlling access through public interfaces.

class Car:
    def __init__(self, speed):
        self.__speed = speed

    def get_speed(self):
        return self.__speed

    def set_speed(self, new_speed):
        self.__speed = new_speed

car = Car(100)
print(car.get_speed())  # Output: 100
car.set_speed(150)
print(car.get_speed())  # Output: 150

solveurit24@gmail.com Changed status to publish February 13, 2025
0