Python Encapsulation
Encapsulation is about protecting data inside a class.
It means keeping data (properties) and methods together in a class, while controlling
how the data can be accessed from outside the class.
This prevents accidental changes to your data and hides the internal details of how your
class works.
Private Properties
In Python, you can make properties private by using a double underscore __ prefix:
ExampleGet your own Python Server
Create a private class property named __age:
class Person:
def __init__(self, name, age):
[Link] = name
self.__age = age # Private property
p1 = Person("Emil", 25)
print([Link])
print(p1.__age) # This will cause an error
Note: Private properties cannot be accessed directly from outside the class.
Get Private Property Value
To access a private property, you can create a getter method:
Example
Use a getter method to access a private property:
class Person:
def __init__(self, name, age):
[Link] = name
self.__age = age
def get_age(self):
return self.__age
p1 = Person("Tobias", 25)
print(p1.get_age())
Set Private Property Value
To modify a private property, you can create a setter method.
The setter method can also validate the value before setting it:
Example
Use a setter method to change a private property:
class Person:
def __init__(self, name, age):
[Link] = name
self.__age = age
def get_age(self):
return self.__age
def set_age(self, age):
if age > 0:
self.__age = age
else:
print("Age must be positive")
p1 = Person("Tobias", 25)
print(p1.get_age())
p1.set_age(26)
print(p1.get_age())
Python Inner Classes
An inner class is a class defined inside another class. The inner class can access the
properties and methods of the outer class.
Inner classes are useful for grouping classes that are only used in one place, making
your code more organized.
ExampleGet your own Python Server
Create an inner class:
class Outer:
def __init__(self):
[Link] = "Outer Class"
class Inner:
def __init__(self):
[Link] = "Inner Class"
def display(self):
print("This is the inner class")
outer = Outer()
print([Link])
Accessing Inner Class from the Outside
To access the inner class, create an object of the outer class, and then create an object
of the inner class:
Example
Access the inner class and create an object:
class Outer:
def __init__(self):
[Link] = "Outer"
class Inner:
def __init__(self):
[Link] = "Inner"
def display(self):
print("Hello from inner class")
outer = Outer()
inner = [Link]()
[Link]()
REMOVE ADS
Accessing Outer Class from Inner Class
Inner classes in Python do not automatically have access to the outer class instance.
If you want the inner class to access the outer class, you need to pass the outer class
instance as a parameter:
Example
Pass the outer class instance to the inner class:
class Outer:
def __init__(self):
[Link] = "Emil"
class Inner:
def __init__(self, outer):
[Link] = outer
def display(self):
print(f"Outer class name: {[Link]}")
outer = Outer()
inner = [Link](outer)
[Link]()
Practical Example
Inner classes are useful for creating helper classes that are only used within the context
of the outer class:
Example
Use an inner class to represent a car's engine:
class Car:
def __init__(self, brand, model):
[Link] = brand
[Link] = model
[Link] = [Link]()
class Engine:
def __init__(self):
[Link] = "Off"
def start(self):
[Link] = "Running"
print("Engine started")
def stop(self):
[Link] = "Off"
print("Engine stopped")
def drive(self):
if [Link] == "Running":
print(f"Driving the {[Link]} {[Link]}")
else:
print("Start the engine first!")
car = Car("Toyota", "Corolla")
[Link]()
[Link]()
[Link]()
Multiple Inner Classes
A class can have multiple inner classes:
Example
Create multiple inner classes:
class Computer:
def __init__(self):
[Link] = [Link]()
[Link] = [Link]()
class CPU:
def process(self):
print("Processing data...")
class RAM:
def store(self):
print("Storing data...")
computer = Computer()
[Link]()
[Link]()