---
title: "What is meant by encapsulation in python?"  
description: "What is meant by encapsulation in python?"  
author: "Amrita Bhattacharjee"  
published: 2023-03-28  
updated: 2023-04-20  
canonical: https://www.mindstick.com/forum/157638/what-is-meant-by-encapsulation-in-python  
category: "python"  
tags: ["python"]  
reading_time: 3 minutes  

---

# What is meant by encapsulation in python?

What is meant by [encapsulation](https://www.mindstick.com/articles/12229/encapsulation-and-access-specifier) in [python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python)?Give suitable example.

## Replies

### Reply by Aryan Kumar

Encapsulation is one of the fundamental concepts in object-oriented programming, which refers to the practice of hiding the implementation details of a class from the outside world and providing a public interface for interacting with it.

In Python, encapsulation is achieved through the use of access modifiers, which are special keywords that control the visibility of class attributes and methods.

There are three access modifiers in Python:

**Public:** The attributes and methods that are marked as public can be accessed from anywhere, both inside and outside the class.

**Protected:** The attributes and methods that are marked as protected can only be accessed from within the class and its subclasses.

**Private:** The attributes and methods that are marked as private can only be accessed from within the class. They cannot be accessed from outside the class, not even by its subclasses.

The access modifiers in Python are implemented using the following conventions:

1. Public attributes and methods are declared without any underscores at the beginning of their names.
2. Protected attributes and methods are declared with a single underscore (_) at the beginning of their names.
3. Private attributes and methods are declared with double underscores (__) at the beginning of their names.

Here is an example of encapsulation in Python:

```python
class BankAccount:
   def __init__(self, account_number, balance):
       self._account_number = account_number
       self._balance = balance
   def deposit(self, amount):
       self._balance += amount
   def withdraw(self, amount):
       if self._balance >= amount:
           self._balance -= amount
       else:
           print("Insufficient balance!")
   def get_balance(self):
       return self._balance
# Create a new instance of BankAccount
account = BankAccount("1234567890", 1000)
# Deposit some money into the account
account.deposit(500)
# Withdraw some money from the account
account.withdraw(200)
# Get the current balance of the account
balance = account.get_balance()
# Print the balance
print(f"Current balance: {balance}")
```

### Reply by Krishnapriya Rajeev

Python is an object-oriented language, in which the objects are functions, classes, strings and even types. These may have a type, can be passed as function arguments and may have methods and properties.

Encapsulation involves wrapping the data (variables) and the code (methods) acting on said data together as a single unit. In encapsulation, data inside one class will be hidden from other classes, and can only be accessed by methods of the same class. This is one of the key concepts of object-oriented languages like Python.

A class can be considered an example of encapsulation, as it contains variables and member functions. An example of a class in Python is given below:

```plaintext
class House:
def __init__(self, location, price):
 self.location = location
 self.price = price

# member function definition
def printdetails(self):
 print("Location: "+self.location)
 print("Price: "+self.price);

# create 2 objects
house1 = House("Delhi",75000)
house2 = House("Mumbai",50000)

# calling the member function
house1.printdetails()
house2.printdetails()
```

We can understand encapsulation better through access modifiers in Python:\
1. Protected members: These are the members of a class that cannot be accessed outside the class but can be accessed from within the class and its subclasses. We can accomplish this by prefixing the name of the member with a single underscore.

2. Private members: These are the members of a class that cannot be accessed outside the class nor by any of its subclasses. To define a private member, we have to prefix the name of the member with a double underscore.

Example:

```plaintext
class Details:
def __init__(self):
 self._id = 1021   # protected member
 self.__password = abc@123 # private member
```


---

Original Source: https://www.mindstick.com/forum/157638/what-is-meant-by-encapsulation-in-python

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
