Access Modifiers in Python: =========================== Modifier: --------- => Modifier is a word which can change the meaning of other word. Ex: Police Traffic Police Types of modifiers ------------------- => two types of modifiers: 1) Access modifiers 2) Non-Access modifiers => Access modifiers are used to define the scope the members of the class. => Three access modifiers: 1) Public access modifier 2) Private access modifier 3) Protected access modifier Note: ------ Like java and other languages there are no keywords available in python to define the access modifiers. public access modifiers: ------------------------- => default in python is called as "public access modifier" when we can define any data member in a class without any symbol considered as "public". => Scope: accessible from anywhere class MyClass: # self.x = 100 y = 200 def __init__(self): self.x = 100 mc = MyClass() print(mc.x) print(mc.y) protected members: ------------------ => these can be define with single underscore (_) => meant for internal use of the class and its subclasses class MyClass: _y = 123 def __init__(self): self. _x = 132 mc = MyClass() print(mc._y) print(mc._x) private members: ---------------- => private members can define with double underscore sign (__) => these can allow to access within the same class. class MyClass: __x = 123 def __init__(self): self.__y = 321 def display(self): print("Private member = ",self.__x) print("Private member = ",self.__y) mc = MyClass() mc.display() # print(mc.__x) Do we able to apply access modifiers to methods of the class? -------------------------------------------------------------- class MyClass: # public method def printing(self): print("hello") def _wish(self): print("Good evening") def __show(self): print("Welcome To Ashok IT!") def _display(self): self.__show() mc = MyClass() mc.printing() mc._wish() # mc.__show() mc._display() class as Public: ---------------- # this is the public class class MyClass: def __init__(self): print("Good evening") mc = MyClass() class as protected: ------------------- # this is the protected class class _MyClass: def __init__(self): print("Good evening") mc = _MyClass() class as private: ----------------- # this is the private class class __MyClass: def __init__(self): print("Good evening") self.x = 100 mc = __MyClass() print(mc.x) Example: -------- # Car Dashboard system class Car: def __init__(self, brand, model): # public members self.brand = brand self.model = model # protected self._engine_number = "ENG12345" # private members self.__password = "car@123" def show_info(self): print(f"Car: {self.brand},Engine : {self._engine_number}") def __start_engine(self): print("Engine started securely with password check....") def start(self,password): if password == self.__password: self.__start_engine() else: print("Wrong password") car1 = Car("Tesla", "Model X") print(car1._engine_number) car1.start("car@123") car1.start("Ravi@123")