Python Tutorial

Python Encapsulation Explained With Examples

Understanding Encapsulation in Python

You can use Python OOPS for restricting access to methods and variables. This is known as encapsulation, because it protects data from direct change. In Python, we use the underscore as a prefix to designate private characteristics, such as single or double.

  • Data Encapsulation in Python

class Computer:

    def __init__(self):

        self.__maxprice = 900

    def sell(self):

        print("Selling Price: {}".format(self.__maxprice))

    def setMaxPrice(self, price):

        self.__maxprice = price

c = Computer()


# change the price

c.__maxprice = 1000


# using setter function




Selling Price: 900
Selling Price: 900
Selling Price: 1000

Video :

Vidoe :

Did you find this article helpful?