Skip to main content

Python Encapsulation

The goal of encapsulation is to protect the data inside a class.

A class keeps its data (properties) and methods in one place and decides how code outside the class may reach that data.

As a result, data is harder to change by accident, and the inner workings of the class stay hidden.


Private Properties

Start a property name with a double underscore, __, and Python treats it as private:

Note: Code outside the class has no direct access to a private property.

Example

A class with a private property called __age:

Prompt
Write a Python class Person with a private __age property. Try to access it from outside the class.
Output: complete Python program

Get Private Property Value

A getter method gives outside code a way to read a private property:

Example

Read a private property through a getter method:

Prompt
Write a Python class Person with a private __age and a getter method get_age. Print the age using the getter.
Output: complete Python program

Set Private Property Value

Changing a private property works the same way, through a setter method.

A setter can also check the new value before storing it:

Example

Change a private property through a setter method:

Prompt
Write a Python class Person with getter and setter methods for private __age, with validation in the setter.
Output: complete Python program

Why Use Encapsulation?

Encapsulation gives you several benefits:

  • Data Protection: Data is safe from accidental changes
  • Validation: New values can be checked before they are stored
  • Flexibility: The inner code can change while code outside the class keeps working
  • Control: You alone decide how data is read and changed

Example

Protect a value and check it with encapsulation:

Prompt
Write a Python Student class with private __grade, set_grade with validation, get_grade, and get_status methods.
Output: complete Python program

Protected Properties

For protected properties, Python has a convention: start the name with one underscore, _:

Note: This is only a convention, and Python does not enforce it. The _ simply signals to other programmers that the property is meant for internal use.

Example

A class with a protected property:

Prompt
Write a Python class Person with a protected _salary property. Show that it can still be accessed.
Output: complete Python program

Private Methods

The double underscore prefix also works on methods, making them private:

Note: A private method behaves like a private property: outside code cannot call it directly. Here, __validate is available only to other methods of the class.

Example

A class with a private method:

Prompt
Write a Python Calculator class with a private __validate method used internally by add method.
Output: complete Python program

Name Mangling

Name mangling is the technique Python uses to make properties and methods private.

A name that starts with double underscores __ is quietly changed: Python puts _ClassName in front of it.

So __age turns into _Person__age.

Note: The mangled name does give you access to a private property (you can use it), but you should avoid it, because it defeats encapsulation's purpose.

Example

Look at a mangled name in action:

Prompt
Write a Python class Person with private __age. Access it using name mangling _Person__age.
Output: complete Python program