Python Guide Sidebar

Python – Access Modifiers

Introduction

Python access modifiers define the visibility and accessibility of class attributes and methods. They help enforce encapsulation by restricting direct access to sensitive data. In Python, there are three primary types of access modifiers:

  • Public – These members are accessible from anywhere in the program.
  • Protected – They can be accessed within the class and its subclasses.
  • Private – These are accessible only within the class itself.

Therefore, understanding these access levels is essential for designing secure, modular, and well-structured object-oriented programs.

4os.

1. Public Access Modifiers in Python

A public attribute or method allows access from anywhere in the program, both inside and outside the class.

Example

class Student:
    def __init__(self, name, age):
        self.name = name  # Public attribute
        self.age = age    # Public attribute

    def display(self):
        print(f"Name: {self.name}, Age: {self.age}")

# Creating an object
s1 = Student("Alice", 20)
print(s1.name)  # Accessible
s1.display()  # Accessible

Key Point: Public attributes are freely accessible from outside the class.

Output

Alice
Name: Alice, Age: 20

2. Protected Access Modifiers in Python

A single underscore (_) indicates a protected attribute or method.The class and its subclasses can still access it; however, it should not be accessed directly.

Example

class Person:
    def __init__(self, name, age):
        self._name = name  # Protected attribute
        self._age = age    # Protected attribute

    def _display(self):  # Protected method
        print(f"Name: {self._name}, Age: {self._age}")

class Employee(Person):
    def show(self):
        print(f"Employee Name: {self._name}")

# Creating an object
e1 = Employee("Bob", 25)
e1.show()  # Accessible
print(e1._name)  # Possible but not recommended

Output

Employee Name: Bob
Bob

Key Point: Protected members should not be accessed directly, but subclasses can still use them.

3. Private Access Modifiers in Python

A private attribute or method is indicated by a double underscore (__). Since direct access outside the class is not allowed, it helps in enforcing encapsulation.

Example

class BankAccount:
    def __init__(self, balance):
        self.__balance = balance  # Private attribute

    def __show_balance(self):  # Private method
        print(f"Balance: {self.__balance}")

    def get_balance(self):
        return self.__balance  # Accessing private attribute within the class

# Creating an object
acc = BankAccount(5000)
# print(acc.__balance)  # This will raise an AttributeError
print(acc.get_balance())  # Access through a method

Output

5000

Key Point: Outside access to private members is restricted, but class methods can access them.

Accessing Private Attributes Using Name Mangling

Although direct access to private attributes is restricted, Python offers a workaround called name mangling.

Example

class Test:
    def __init__(self):
        self.__private_var = 42  # Private variable

obj = Test()
print(obj._Test__private_var)  # Name mangling to access private variable

Output

42

Key Point: Name mangling allows accessing private attributes but should be used cautiously.

Conclusion

Python’s access modifiers play a crucial role in controlling how class attributes and methods are accessed.

  • Firstly, public members can be accessed from anywhere in the program.
  • Meanwhile, protected members are accessible within the class and its subclasses, though they should not be accessed directly.
  • On the other hand, private members are restricted to the class and can only be accessed using class methods or name mangling.

Therefore, using access modifiers properly not only enhances encapsulation but also improves data security and code maintainability.

Additional Resources