Python allows us to define private attributes in our class (private instance variables). Outside the scope of the class it won’t be possible to access them. In order to create private attributes we should prefix their names with two underscores (‘__’).

class Rectangle:
    def __init__(self,w=10,h=10):
        self.__width = w
        self.__height = h
    def area(self):
        return self.__width*self.__height

ob = Rectangle(3,4)
print(ob.area())
ob.__width = 10
print(ob.area())

The following video clip overviews this code sample and explains it step by step.

Leave a Reply

Your email address will not be published. Required fields are marked *