Understanding Access Modifiers in Python: Public, Protected, and Private Explained

surajkumarTpT

New member
Sep 4, 2025
11
0
1
In Python, Access modifiers for Python are used to define the scope and visibility of class members (variables and methods). They help in implementing the concept of encapsulation in object-oriented programming. Unlike some languages, Python doesn’t enforce strict rules but follows a convention-based approach.


  1. Public Members:
    By default, all members in Python are public. They can be accessed from anywhere in the program. Example:


class MyClass:
def __init__(self):
self.name = "Public"


  1. Protected Members:
    Members prefixed with a single underscore _variable are considered protected. They should not be accessed directly outside the class, but Python still allows it. This is more of a convention.


self._age = 25


  1. Private Members:
    Members with double underscores __variable are treated as private. Python uses name mangling to make them harder to access outside the class.


self.__salary = 50000


In short, Python relies on naming conventions rather than strict enforcement. Developers are expected to follow these practices to maintain clean, secure, and understandable code.
 

Attachments

  • Access Modifiers in Python.png
    Access Modifiers in Python.png
    36.4 KB · Views: 0