Lesson 3
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).

Python
1class Player: 2 def __init__(self, health, armor, stamina): 3 self.health = health 4 self.armor = armor 5 self.stamina = stamina 6 7 def receive_damage(self, damage): 8 self.health -= damage # Reduce health 9 10 def shield_hit(self, armor): 11 self.armor -= armor # Decrease armor 12 13 def restore_health(self, health_increase): 14 self.health += health_increase # Restore health 15 16player = 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.

Python
1class BankAccount: 2 def __init__(self, account_number, balance): 3 self.account_number = account_number 4 self.__balance = balance # Private attribute 5 6 def deposit(self, amount): 7 self.__balance += amount # Deposit money 8 9bank_account = BankAccount(1234, 100) 10print(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:

Python
1class BankAccount: 2 def __init__(self, account_number, balance): 3 self.account_number = account_number 4 self.__balance = balance 5 6 # Private method 7 def __add_interest(self, interest_rate): 8 self.__balance += self.__balance * interest_rate # Calculation of interest 9 10 # Public method calling the private method 11 def add_yearly_interest(self): 12 self.__add_interest(0.02) # Adds 2% interest 13 14bank_account = BankAccount(1234, 100) 15bank_account.add_yearly_interest() # Works for public methods 16bank_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.

Remark the Privacy

In Python, a double underscore __ before the attribute or method name designates it as private.

Python
1class PrivateExample: 2 def __init__(self): 3 self.public_attribute = "Public" 4 self.__private_attribute = "Private" # Private attribute

Private attributes and methods are inaccessible directly from an instance. This arrangement helps maintain integrity.

Lesson Summary and Practice

Great job refreshing your understanding of encapsulation, private attributes, and private methods concepts in Python! Correctly understanding and applying these foundational principles of OOP make your code concise, robust, and secure.

Coming up next is hands-on practice. Keep up the good work – exciting exercises are just around the corner!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.