Encapsulation and Privacy in Python: Securing Your Classes

Lesson Overview

Hello! In this lesson, we're revisiting Encapsulation, Private Attributes, and Private Methods in Object-Oriented Programming (OOP). Imagine encapsulation as an invisible fence safeguarding a garden from outside interference, keeping data and methods safe within. Within this garden, certain plants (Private Attributes and Methods) are only for the gardener's eyes. These are crucial for making your classes more robust and secure!

Into the Encapsulation

Encapsulation in OOP wraps up data and methods into a class. This organizational approach tidies the code and reinforces security. If you were to code a multiplayer game, for example, you could create a Player class, encapsulating data (health, armor, stamina) and methods (receive_damage, shield_hit, restore_health).

class Player:
    def __init__(self, health, armor, stamina):
      self.health = health
      self.armor = armor
      self.stamina = stamina

    def receive_damage(self, damage):
      self.health -= damage  # Reduce health

    def shield_hit(self, armor):
      self.armor -= armor  # Decrease armor

    def restore_health(self, health_increase):
      self.health += health_increase  # Restore health

player = Player(100, 50, 77)

Now, player is an instance of the Player class on which you can call methods.

Private Attributes

Private Attributes, which can only be altered via class methods, limit outside interference. For instance, a BankAccount class might feature a balance private attribute that one could change only through deposits or withdrawals.

class BankAccount:
    def __init__(self, account_number, balance):
        self.account_number = account_number
        self.__balance = balance  # Private attribute
    
    def deposit(self, amount):
        self.__balance += amount  # Deposit money

bank_account = BankAccount(1234, 100)
print(bank_account.__balance) # Error: can't access private attribute

Here, __balance is private, thus ensuring the integrity of the account balance.

Private Methods

Like private attributes, private methods are accessible only within their class. Here's an example:

class BankAccount:
    def __init__(self, account_number, balance):
        self.account_number = account_number
        self.__balance = balance

    # Private method
    def __add_interest(self, interest_rate):
        self.__balance += self.__balance * interest_rate  # Calculation of interest

    # Public method calling the private method
    def add_yearly_interest(self):
        self.__add_interest(0.02)  # Adds 2% interest

bank_account = BankAccount(1234, 100)
bank_account.add_yearly_interest() # Works for public methods
bank_account.__add_interest(0.1) # Error: can't call a private method

Here, add_yearly_interest is a public method that calls the private method __add_interest.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal