Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons

© 2026 BraveProgrammer. All rights reserved.

  1. Courses
  2. /
  3. Python Fundamentals

Lesson 28 of 55 · Python

OOP – Encapsulation

Duration: 20 minutes

OOP – Encapsulation\n\nEncapsulation hides an object's internal state and forces interaction through a defined public interface. In Python, a leading double underscore triggers name‑mangling, making an attribute harder to access from outside.\n\npython\nclass BankAccount:\n def __init__(self, balance=0):\n self.__balance = balance # private attribute\n\n @property\n def balance(self):\n return self.__balance\n\n def deposit(self, amount):\n if amount > 0:\n self.__balance += amount\n\naccount = BankAccount(100)\naccount.deposit(50)\nprint('Balance:', account.balance) # 150\n\n\nThe balance property is read‑only from the outside; only deposit can modify the underlying private attribute.\n

Previous: OOP – PolymorphismNext: Magic Methods and Dunder