Modifiers in Python

Modifers are key words used in class, variable, method and constructor declarations. It decides the scope and visibility of these declarations.

  1. public  – members are accessible outside the class
  2. private – members are accessed only side the class
  3. 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

#class declaration
class Car:
# default constructor
def __init__(self):
self.model = “Maruthi”
self.__color_ = “Red”
# private variable cannot accessed from outside the class
# In python is implemented by using name mangling concept

def showCarInfo(self):
print (“Car Details”)
print (“Car Model :”, self.model)
print(“Car Color : “, self.__color_)

#object creation

# Accessing the private variable from outside the class

Win Corporate Training
View My Profile