Python Access Modifiers

Python Access Modifiers: Understanding Encapsulation

When working with object-oriented programming (OOP) languages like Python, encapsulation is an essential principle to ensure data integrity and code maintainability. Access modifiers play a crucial role in implementing encapsulation by controlling the visibility and accessibility of class members, such as variables and methods.

Public Access Modifier

The public access modifier is the default in Python, meaning that class members are accessible from anywhere within the program. Public members can be accessed and modified directly, without any restrictions. Let’s consider an example:

“`python
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model

def start_engine(self):
print(f”The {self.brand} {self.model} engine is starting.”)

my_car = Car(“Toyota”, “Camry”)
print(my_car.brand) # Output: Toyota
my_car.start_engine() # Output: The Toyota Camry engine is starting.
“`

In this example, the `brand` and `model` variables are public, allowing us to access and modify them directly using the dot notation. Similarly, the `start_engine` method is also accessible from outside the class.

Protected Access Modifier

The protected access modifier is denoted by a single underscore (_) before the member name. Protected members are intended to be accessed within the class itself or its subclasses. Although it is not enforced by the language, it serves as a naming convention to indicate that the member should not be accessed from outside the class hierarchy. Let’s see an example:

“`python
class Person:
def __init__(self, name, age):
self._name = name
self._age = age

def display_info(self):
print(f”Name: {self._name}, Age: {self._age}”)

class Employee(Person):
def __init__(self, name, age, salary):
super().__init__(name, age)
self._salary = salary

def display_info(self):
super().display_info()
print(f”Salary: ${self._salary}”)

employee = Employee(“John Doe”, 30, 5000)
employee.display_info()
“`

In this example, the `_name` and `_age` variables are marked as protected. Although they can still be accessed from outside the class, it is considered good practice to treat them as non-public members. The `display_info` method is also accessible from outside the class hierarchy, but it is typically called through an instance of the `Employee` class.

Private Access Modifier

The private access modifier is denoted by a double underscore (__) before the member name. Private members are intended to be accessed only within the class that defines them. They cannot be accessed directly from outside the class, including its subclasses. Let’s examine an example:

“`python
class BankAccount:
def __init__(self, account_number, balance):
self.__account_number = account_number
self.__balance = balance

def withdraw(self, amount):
if amount > self.__balance:
print(“Insufficient funds.”)
else:
self.__balance -= amount
print(f”Withdrew ${amount}. New balance: ${self.__balance}”)

def __display_balance(self):
print(f”Account balance: ${self.__balance}”)

account = BankAccount(“1234567890”, 1000)
account.withdraw(500) # Output: Withdrew $500. New balance: $500
account.__display_balance() # Error: ‘BankAccount’ object has no attribute ‘__display_balance’
“`

In this example, the `__account_number` and `__balance` variables are marked as private. They cannot be accessed directly from outside the class, resulting in an error when trying to call the `__display_balance` method. Private members are typically accessed indirectly through public methods defined within the class.

Conclusion

Access modifiers in Python provide a means to enforce encapsulation and control the visibility of class members. By using public, protected, and private access modifiers, you can determine which parts of your code are accessible from outside the class and its subclasses. This promotes code reusability, maintainability, and data integrity, ultimately leading to more robust and scalable applications.

Scroll to Top