class in python

amitmund June 04, 2026

Python Classes - Complete Notes

Table of Contents

  1. Introduction to OOP
  2. Classes and Objects
  3. Constructor (__init__)
  4. Understanding self
  5. Attributes
  6. Methods
  7. Instance Variables
  8. Class Variables
  9. Class Methods
  10. Static Methods
  11. Encapsulation
  12. Getters and Setters
  13. Inheritance
  14. Method Overriding
  15. super()
  16. Polymorphism
  17. Magic Methods
  18. Operator Overloading
  19. Abstract Classes
  20. Composition
  21. Dataclasses
  22. Design Patterns
  23. Metaclasses
  24. Interview Questions
  25. Practice Exercises

1. Introduction to OOP

What is OOP?

Object-Oriented Programming (OOP) is a programming paradigm that organizes code using objects and classes.

Benefits

  • Code Reusability
  • Modularity
  • Scalability
  • Maintainability
  • Security through Encapsulation

Four Pillars of OOP

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

Notes

Add your understanding here.


2. Classes and Objects

Definition

A class is a blueprint for creating objects.

Example

class Student:
    pass

Creating Objects

s1 = Student()
s2 = Student()

Notes


3. Constructor (__init__)

Definition

Constructor is automatically executed when an object is created.

Example

class Student:
    def __init__(self):
        print("Object Created")

Notes


4. Understanding self

Definition

self refers to the current object.

Example

class Student:
    def __init__(self, name):
        self.name = name

Key Points

  • Refers to current instance
  • First parameter of instance methods
  • Passed automatically by Python

Notes


5. Attributes

Instance Attributes

class Student:
    def __init__(self, name):
        self.name = name

Class Attributes

class Student:
    school = "ABC School"

Notes


6. Methods

Instance Method

class Student:
    def greet(self):
        print("Hello")

Notes


7. Instance Variables

Definition

Variables unique to each object.

Example

class Dog:
    def __init__(self, name):
        self.name = name

Notes


8. Class Variables

Definition

Shared among all objects.

Example

class Student:
    school = "ABC School"

Notes


9. Class Methods

Syntax

@classmethod
def method(cls):
    pass

Example

class Student:
    school = "ABC"

    @classmethod
    def get_school(cls):
        return cls.school

Notes


10. Static Methods

Syntax

@staticmethod
def method():
    pass

Example

class Math:
    @staticmethod
    def add(a, b):
        return a + b

Notes


11. Encapsulation

Definition

Restrict direct access to data.

Example

class Bank:
    def __init__(self):
        self.__balance = 1000

Notes


12. Getters and Setters

Getter

def get_balance(self):
    return self.__balance

Setter

def set_balance(self, amount):
    self.__balance = amount

Notes


13. Inheritance

Definition

Acquire properties from another class.

Example

class Animal:
    pass

class Dog(Animal):
    pass

Types

  • Single
  • Multiple
  • Multilevel
  • Hierarchical
  • Hybrid

Notes


14. Method Overriding

Example

class Animal:
    def sound(self):
        print("Animal")

class Dog(Animal):
    def sound(self):
        print("Bark")

Notes


15. super()

Purpose

Call parent class methods.

Example

class Dog(Animal):
    def __init__(self):
        super().__init__()

Notes


16. Polymorphism

Definition

One interface, multiple implementations.

Example

class Dog:
    def sound(self):
        print("Bark")

class Cat:
    def sound(self):
        print("Meow")

Notes


17. Magic Methods

Common Magic Methods

Method Purpose
__init__ Constructor
__str__ String Representation
__len__ Length
__add__ Addition
__eq__ Equality

Example

def __str__(self):
    return self.name

Notes


18. Operator Overloading

Example

def __add__(self, other):
    return self.value + other.value

Notes


19. Abstract Classes

Example

from abc import ABC, abstractmethod

class Animal(ABC):

    @abstractmethod
    def sound(self):
        pass

Notes


20. Composition

Definition

One class contains another class.

Example

class Engine:
    pass

class Car:
    def __init__(self):
        self.engine = Engine()

Notes


21. Dataclasses

Example

from dataclasses import dataclass

@dataclass
class Student:
    name: str
    age: int

Benefits

  • Less boilerplate code
  • Auto-generated methods

Notes


22. Design Patterns

Patterns to Learn

  • Singleton
  • Factory
  • Observer
  • Strategy
  • Adapter
  • Decorator

Notes


23. Metaclasses

Definition

A class that creates classes.

Example

class MyMeta(type):
    pass

Notes


24. Interview Questions

Basic

  • What is OOP?
  • What is a class?
  • What is an object?
  • What is self?

Intermediate

  • Difference between class and instance variable?
  • Difference between classmethod and staticmethod?
  • What is encapsulation?

Advanced

  • What are magic methods?
  • What is MRO?
  • What are metaclasses?

My Answers


25. Practice Exercises

Beginner

  • Create Student class
  • Create Car class
  • Create BankAccount class

Intermediate

  • Build Employee Management System
  • Build Library Management System

Advanced

  • Build E-Commerce System
  • Build Inventory Management System
  • Build School ERP

Personal Revision Notes

Important Concepts

  • self
  • init
  • Class Variables
  • Class Methods
  • Static Methods
  • Inheritance
  • Polymorphism
  • Encapsulation
  • Magic Methods
  • Abstract Classes

Revision Date

Date Topic Status

Questions To Ask Later

1. 2. 3. 4. 5.


Useful Resources

  • Python Official Documentation
  • OOP Concepts
  • Design Patterns
  • Django Models (uses OOP heavily)

Summary

Classes are blueprints for objects.

Core topics to master:

  • Class
  • Object
  • Constructor
  • self
  • Methods
  • Variables
  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction
  • Magic Methods

Master these and you'll be comfortable building real-world Python applications.

0 Likes
8 Views

Filters

No filters available for this view.

Reset All