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:
Public attributes and methods are declared without any underscores at the beginning of their names.
Protected attributes and methods are declared with a single underscore (_) at the beginning of their names.
Private attributes and methods are declared with double underscores (__) at the beginning of their names.
Here is an example of encapsulation in 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}")
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:
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:
class Details:
def __init__(self):
self._id = 1021 # protected member
self.__password = abc@123 # private member
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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:
Here is an example of encapsulation in Python:
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:
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: