Python Tutorials · Python Encapsulation

Python Encapsulation

Learn all about Python Encapsulation in this comprehensive tutorial.

5 min read advanced
  • Encapsulation is about protecting data inside a class.
  • In Python, you can make properties private by using a double underscore __ prefix:
  • To access a private property, you can create a getter method:
  • To modify a private property, you can create a setter method.
  • Encapsulation provides several benefits:
  • Python also has a convention for protected properties using a single underscore _ prefix:
  • You can also make methods private using the double underscore prefix:
  • Name mangling is how Python implements private properties and methods.

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:

python
Note: 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:

python

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:

python

Why Use Encapsulation?

Encapsulation provides several benefits:

  • **Data Protection:** Prevents accidental modification of data
  • **Validation:** You can validate data before setting it
  • **Flexibility:** Internal implementation can change without affecting external code
  • **Control:** You have full control over how data is accessed and modified
python

Protected Properties

Python also has a convention for protected properties using a single underscore _ prefix:

python
Note: Note: A single underscore _ is just a convention. It tells other programmers that the property is intended for internal use, but Python doesn't enforce this restriction.

Private Methods

You can also make methods private using the double underscore prefix:

python
Note: Note: Just like private properties with double underscores, private methods cannot be called directly from outside the class. The __validate method can only be used by other methods inside the class.

Name Mangling

Name mangling is how Python implements private properties and methods.

When you use double underscores __, Python automatically renames it internally by adding _ClassName in front.

For example, __age becomes _Person__age.

python
Note: Note: While you can access private properties using the mangled name, it's not recommended. It defeats the purpose of encapsulation.

Module quiz

2 questions
1

Which of the following is true about Python Encapsulation?

2

What is the most common pitfall when working with Python Encapsulation?

Answer all questions to submit.