Sunday, July 7, 2024

Encapsulation in Python

What is Encapsulation?

Encapsulation is an OOP principle that involves bundling the data (attributes) and methods (functions) that operate on the data into a single unit, known as a class. It also involves restricting direct access to some of the object's components, which is a way of preventing accidental interference and misuse of the data.

Key Points of Encapsulation:

1. **Data Hiding**: Encapsulation allows hiding the internal state of an object and requiring all interaction to be performed through an object's methods.

2. **Controlled Access**: Provides controlled access to the attributes and methods of an object, usually through public methods.

3. **Modularity**: Improves modularity by keeping the data safe from outside interference and misuse.

Example of Encapsulation

Let’s go through an example to understand encapsulation better.

Step 1: Define a Class

```

class Person:

    def __init__(self, name, age):

        self.name = name        # Public attribute

        self.__age = age        # Private attribute


    def get_age(self):

        return self.__age       # Public method to access private attribute


    def set_age(self, age):

        if age > 0:

            self.__age = age    # Public method to modify private attribute

        else:

            print("Age must be positive!")

```

In this example:

- `name` is a public attribute, meaning it can be accessed directly.

- `__age` is a private attribute, indicated by the double underscore prefix (`__`). It cannot be accessed directly from outside the class.

- `get_age` and `set_age` are public methods that provide controlled access to the private attribute `__age`.

Step 2: Create an Object of the Class

```

person = Person("Alice", 30)

print(person.name)     # Output: Alice

print(person.get_age())  # Output: 30

```

Here, `person` is an instance of the `Person` class:

- We can access the `name` attribute directly because it is public.

- We access the `__age` attribute using the `get_age` method because `__age` is private.

Step 3: Modify the Private Attribute

```

person.set_age(35)

print(person.get_age())  # Output: 35


person.set_age(-5)       # Output: Age must be positive!

print(person.get_age())  # Output: 35

```

- We modify the `__age` attribute using the `set_age` method.

- The method includes a check to ensure the age is positive, demonstrating controlled access.


Access Modifiers in Python


1. Public Members: Accessible from anywhere.

   - Example: `self.name`

2. Private Members: Accessible only within the class.

   - Example: `self.__age`

3. Protected Members: Indicated by a single underscore (e.g., `self._name`). These are a convention and are intended to be accessed only within the class and its subclasses, but not enforced by Python.

```

class Person:

    def __init__(self, name, age):

        self.name = name        # Public attribute

        self._address = None    # Protected attribute

        self.__age = age        # Private attribute

```

 Summary

- **Encapsulation** bundles data and methods that operate on the data into a single unit (class).

- It hides the internal state of an object and requires all interaction to be performed through an object's methods.

- **Public Members**: Accessible from anywhere.

- **Private Members**: Accessible only within the class, using a double underscore prefix (`__`).

- **Protected Members**: Indicated by a single underscore (`_`), intended for internal use within the class and subclasses.

Encapsulation helps in protecting the data from unauthorized access and modification, providing a clear and controlled way to interact with the object's attributes.

No comments:

AI's Impact on the IT Industry 2026