PYnative

Python Programming

  • Learn Python
    • Python Tutorials
    • Python Basics
    • Python Interview Q&As
  • Exercises
    • Python Exercises
    • C Programming Exercises
    • C++ Exercises
  • Quizzes
  • Code Editor
    • Online Python Code Editor
    • Online C Compiler
    • Online C++ Compiler
Home » Python » Python Object-Oriented Programming (OOP) » Encapsulation in Python

Encapsulation in Python

Updated on: August 28, 2021 | 28 Comments

Encapsulation is one of the fundamental concepts in object-oriented programming (OOP), including abstraction, inheritance, and polymorphism. This lesson will cover what encapsulation is and how to implement it in Python.

After reading this article, you will learn:

  • Encapsulation in Python
  • Need for Encapsulation
  • Data Hiding using public, protected, and private members
  • Data Hiding vs. Encapsulation
  • Getter and Setter Methods
  • Benefits of Encapsulation

Table of contents

  • What is Encapsulation in Python?
  • Access Modifiers in Python
  • Public Member
  • Private Member
    • Public method to access private members
    • Name Mangling to access private members
  • Protected Member
  • Getters and Setters in Python
  • Advantages of Encapsulation

What is Encapsulation in Python?

Encapsulation in Python describes the concept of bundling data and methods within a single unit. So, for example, when you create a class, it means you are implementing encapsulation. A class is an example of encapsulation as it binds all the data members (instance variables) and methods into a single unit.