Modifers are key words used in class, variable, method and constructor declarations. It decides the scope and visibility of these declarations.
- public – members are accessible outside the class
- private – members are accessed only side the class
- protected – members are accessed by same class and its sub classes.
[Note: Real time applications Class, Method and Constructors will be public and Variables will be private. It is used to implement Encapsulation concept in Object Oriented programming.]
Python class members are public by default. No keywords are used to represent modifiers in Python. Modifiers will be specified by using some Techniques.
private Variable Declaration in Python
self.__empno_=2004 Use two underscore before the variable name and one underscore after the variable name.
Example: Mofidiers in Python
# default constructor
self.model = “Maruthi”
self.__color_ = “Red”
# private variable cannot accessed from outside the class
# In python is implemented by using name mangling concept
print (“Car Details”)
print (“Car Model :”, self.model)
print(“Car Color : “, self.__color_)
# Accessing the private variable from outside the class
Win Corporate Training