Are you ready for an exciting Python journey? Today's destination is Encapsulation, a pillar of OOP. Encapsulation bundles related data and methods — much like yolk and egg white are nestled inside an eggshell—within the framework of a class. This lesson will help us understand encapsulation's roles, cover public and private variables, explain getters and setters, and reveal how encapsulation hides implementation details.
Encapsulation harmonizes related variables and functions, protecting them from unauthorized external access. Take a car, for instance — it's an elaborate piece of machinery (data) controlled by various user-friendly gadgets (methods). In Python, an object bundles class variables and functions, thus providing efficient data manipulation and masking complexity.
Classes hosted in Python have variables and methods. Variables, such as the color or model of a car class, can either be public (accessible everywhere) or private (hidden).
Python1class Car: 2 color = 'Red' # public variable 3 __gear_box_type = 'Manual' # private variable
__gear_box_type
is a private variable, you can mention it by the two underscores (__
) at the beginning of its name. To access or modify it, we use getters and setters, which we will discuss in the next section of this lesson.
Getters retrieve attribute values, while setters assign values to them. They protect data from unauthorized changes.
Observe a simple class, Car
, which has a private variable, __gear_box_type
:
Python1class Car: 2 def __init__(self): 3 self.__gear_box_type = 'Manual'
Since __gear_box_type
is a private variable, you cannot access it like you would a public variable. You need to use a getter or setter method.
Python1class Car: 2 def __init__(self): 3 self.__gear_box_type = 'Manual' 4 5 # Getter 6 def get_gear_box_type(self): 7 return self.__gear_box_type 8 9 # Setter 10 def set_gear_box_type(self, gearbox): 11 self.__gear_box_type = gearbox 12 13# Instance 14hyundai = Car() 15print(hyundai.get_gear_box_type()) # Output: Manual 16 17# Change Gearbox Type 18hyundai.set_gear_box_type('Automatic') 19print(hyundai.get_gear_box_type()) # Output: Automatic
Here, we use get_gear_box_type
and set_gear_box_type
to access the private variable inside the Car
class. This is what encapsulation is all about - protecting and limiting access and hiding implementation from the caller.
Encapsulation enhances data security, simplifies debugging, and facilitates code reuse and modification. For example, a car's gear system (denoted by the variable __gear_box_type
) can't silently switch from 'Manual' to 'Pedal Powered', unless you allow it to, implementing the set_gear_box_type
method. This setter can also control field access logic or permissions checks if needed.
Encapsulation conceals complexities by providing a simple interface, much like the way a car hides all the intricate mechanisms from the driver.
Python1class Car: 2 def __init__(self): 3 self.__gear_box_type = 'Manual' 4 5 # Getter 6 def get_gear_box_type(self): 7 return self.__gear_box_type 8 9 # Setter, hiding implementation 10 def set_gear_box_type(self, gearbox): 11 self.__gear_box_type = gearbox
The internals of set_gear_box_type()
are hidden for the method caller, providing a simplified interface. You can use any public methods available in the Car
's interface, but you can't mess with internal data.
Congratulations on understanding Encapsulation in Python! You've learned about encapsulation, public and private variables, getters and setters, and the practicality of encapsulation. Next up are hands-on exercises to help solidify these concepts. Let's tackle them head-on!