Private Attributes in Python

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.

Share:

banner for the css playlist in hebrew life michael courses for programmers

The First Steps in CSS

Learn CSS using our our videos (in Hebrew) on the CSS (he) playlist on youtube. Do it now. Do it for free.

Good Trainers Collaborate with Others

It is always essential to keep an open mind and learn from others. This applies to everyone, including teachers and especially software development trainers. Software

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Update cookies preferences