SwadhyAI Notes

All Python notes

Python OOP 🏗️

Object Oriented Programming

Python is a multi-paradigm language, but it is heavily based on Object-Oriented Programming (OOP). OOP is a programming paradigm based on the concept of "objects", which can contain data (attributes) and code (methods).

✨ Core Concepts

Class

The blueprint or template for creating objects. Think of it like a cookie cutter.

Object

An instance of a class. Think of it like the cookie made from the cutter.

Attribute

Variables that belong to an object (e.g., color, size).

Method

Functions that belong to an object (e.g., eat(), sleep()).

🧱 The 4 Pillars of OOP

OOP is built on four main principles:

  • Encapsulation: Bundling data and methods that work on that data within one unit (class).
  • Abstraction: Hiding complex implementation details and showing only the necessary features of the object.
  • Inheritance: Deriving new classes from existing ones to reuse code.
  • Polymorphism: Using a single interface to represent different data types or classes.

✨ Pro Tip: Why OOP?

OOP helps to keep the code DRY (Don't Repeat Yourself) and makes the code easier to maintain, modify, and debug. It allows you to model real-world entities in your code.