Encapsulation is the practice of hiding complexity inside a “black box” so that it’s easier to focus on the problem at hand in source
The simplest example of encapsulation is a function. The caller of a function doesn’t need to worry too much about what happens inside, they just need to understand the inputs and outputs.

# who even knows how this function works???
# I sure don't, I just call it and assume
# it calculates the acceleration correctly
acceleration: float = calc_acceleration(initial_speed, final_speed, time)

To use the calc_acceleration function, we don’t need to think about each line of code inside the function definition. We just need to know that if we give it the inputs:

  • initial_speed
  • final_speed
  • time

It will produce the correct acceleration as an output.

Public and Private

By default, all properties and methods in a class are public. That means that you can access them with the . operator:

wall.height = 10
print(wall.height)
# 10

Private data members are a way to encapsulate logic and data within a class definition. To make a property or method private just prefix it with two underscores:

class Wall:
    def __init__(self, armor: int, magic_resistance: int) -> None:
        self.__armor = armor
        self.__magic_resistance = magic_resistance
 
    def get_defense(self) -> int:
        return self.__armor + self.__magic_resistance
 
 
front_wall = Wall(10, 20)
 
# This results in an error
print(front_wall.__armor)
 
# This works
print(front_wall.get_defense())
# 30

We do this to make it easier to use our class. Now when another developer (or even ourselves) use the Wall class, they don’t need to think about how armor and magic_resistance affect the defense of a Wall. In fact, we don’t even allow them to access armor and magic_resistance directly by making them private with __.

They simply call the public get_defense() method and know that the correct value will be returned.

Encapsulation is all about making code easy to work with by “hiding” complicated code within a “black box”. It makes it so we don’t have to worry about the details of how a function or method works… we just use it.

This is kinda like how the inner workings of your car’s power steering are hidden from you. You don’t need to be an expert on hydraulic systems to turn the wheel from side to side.

Encapsulation and the concepts of private and public members have NOTHING to do with security. Encapsulation and the concepts of private and public members have NOTHING to do with security

Encapsulation is about organization, not security.