---
title: "Explain the Python Oops Encapsulation with example."  
description: "Explain the Python Oops Encapsulation with example."  
author: "Manish Kumar"  
published: 2025-10-27  
updated: 2025-11-03  
canonical: https://www.mindstick.com/forum/161971/explain-the-python-oops-encapsulation-with-example  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 2 minutes  

---

# Explain the Python Oops Encapsulation with example.

**[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) the [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) [Oops](https://www.mindstick.com/articles/1558/introduction-to-oops-object-oriented-programming-system) [Encapsulation](https://www.mindstick.com/articles/12229/encapsulation-and-access-specifier) with example.**

## Replies

### Reply by ICSM Computer

> **Encapsulation** is one of the core principles of [**Object-Oriented Programming (OOP)**](https://www.mindstick.com/interview/34383/explain-the-python-oop). It means **binding data (variables)** and **methods (functions)** that operate on that data **within a single unit** — a **class**.

It also helps in **restricting direct access** to the internal state of an object — protecting data from unintended modification.

In Python, encapsulation is implemented using:

- **Public members** – accessible from anywhere.
- **Protected members** – prefix with `_` (single underscore), meant for internal use.
- **Private members** – prefix with `__` (double underscore), not accessible directly from outside the class.

## Example of Encapsulation

```python
class BankAccount:
    def __init__(self, owner, balance):
        self.owner = owner          # Public attribute
        self._account_type = "Saving"  # Protected attribute
        self.__balance = balance    # Private attribute

    # Public method
    def deposit(self, amount):
        if amount > 0:
            self.__balance += amount
            print(f"Deposited: {amount}")
        else:
            print("Invalid amount")

    # Public method to view balance (controlled access)
    def get_balance(self):
        return self.__balance

    # Public method to withdraw money
    def withdraw(self, amount):
        if amount <= self.__balance:
            self.__balance -= amount
            print(f"Withdrawn: {amount}")
        else:
            print("Insufficient balance")

# Create object
account = BankAccount("Anna", 5000)

# Access public attribute
print("Owner:", account.owner)

# Access protected attribute (possible but discouraged)
print("Account Type:", account._account_type)

# Access private attribute directly (Not allowed)
# print(account.__balance)  #  AttributeError

# Access private data using public method (Encapsulated access)
print("Balance:", account.get_balance())

account.deposit(1000)
account.withdraw(2000)
print("Updated Balance:", account.get_balance())
```

## Output

```plaintext
Owner: Anna
Account Type: Saving
Balance: 5000
Deposited: 1000
Withdrawn: 2000
Updated Balance: 4000
```

## Why Encapsulation is Important

- **Data protection** — prevents external code from directly changing critical variables.
- **Controlled access** — allows validation or checks before modifying data.
- **Maintainability** — you can change internal implementation without affecting other code.
- **Abstraction support** — hides internal details from users.

## Accessing Private Variables (Name Mangling)

Even though private variables are not directly accessible, Python internally renames them:

```python
print(account._BankAccount__balance)  # Accessible via name mangling (not recommended)
```


---

Original Source: https://www.mindstick.com/forum/161971/explain-the-python-oops-encapsulation-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
