LEARN COMPLETE PYTHON IN 24 HOURS

🟦 Table of Contents – Python OOP

🔹 1. Introduction to Object-Oriented Programming (OOP)

  • 1.1 What is OOP and Why Learn It?

  • 1.2 Real-World Examples – Class vs Object

  • 1.3 The 4 Pillars of OOP in Python

  • 1.4 Procedural vs Object-Oriented Programming

🔹 2. Classes and Objects – Basic Building Blocks

  • 2.1 Creating Classes

  • 2.2 Creating Objects

  • 2.3 The init Method and self Parameter

  • 2.4 Instance Variables vs Class Variables

  • 2.5 str and repr

🔹 3. Encapsulation – Data Hiding and Protection

  • 3.1 Public, Protected and Private Members

  • 3.2 Name Mangling

  • 3.3 @property, @setter, @deleter

🔹 4. Inheritance – Reusing Code

  • 4.1 Single Inheritance

  • 4.2 Using super()

  • 4.3 Method Overriding and Polymorphism

  • 4.4 Multiple Inheritance and MRO

🔹 5. Polymorphism – One Name, Different Behavior

  • 5.1 Method Overriding

  • 5.2 Operator Overloading

  • 5.3 Duck Typing

  • 5.4 Abstract Base Classes

🔹 6. Class Methods, Static Methods and Decorators

  • 6.1 @classmethod

  • 6.2 @staticmethod

  • 6.3 Alternative Constructors

  • 6.4 @property

🔹 7. Advanced OOP Concepts

  • 7.1 Composition vs Inheritance

  • 7.2 Data Classes (@dataclass)

  • 7.3 Magic Methods / Dunder Methods

  • 7.4 Metaclasses

🔹 8. Real-World OOP Projects & Best Practices

  • 8.1 Bank Account System

  • 8.2 Library Management System

  • 8.3 Employee Payroll System

  • 8.4 OOP Best Practices

🔹 9. Common Mistakes & Interview Preparation

  • 9.1 Common OOP Mistakes

  • 9.2 Python OOP Interview Questions

  • 9.3 Debugging OOP Code

🔹 10. Next Steps After Mastering OOP

  • 10.1 Design Patterns

  • 10.2 Decorators and Context Managers

  • 10.3 OOP in FastAPI / Django

  • 10.4 Resources

6. Class Methods, Static Methods and Decorators

In Python, regular instance methods receive self (the instance) as the first argument. But sometimes you need methods that work with the class itself or don't need any instance/class at all. That’s where @classmethod and @staticmethod come in — both are decorators that change how a method is called.

6.1 @classmethod – Receives Class as First Argument

@classmethod makes a method receive the class (not the instance) as the first argument — conventionally named cls.

Use it when you need to work with the class itself (e.g., modifying class variables, creating alternative constructors).

Basic example

Python

class Person: species = "Homo sapiens" # class variable @classmethod def get_species(cls): return cls.species @classmethod def change_species(cls, new_species): cls.species = new_species print(Person.get_species()) # Homo sapiens Person.change_species("Homo superior") print(Person.get_species()) # Homo superior

Key points:

  • First parameter is cls (the class)

  • Can access/modify class variables

  • Can be called on the class or on an instance

  • Very useful for factory methods (alternative constructors)

6.2 @staticmethod – No self, No cls

@staticmethod creates a method that doesn’t receive self or cls — it behaves like a regular function inside the class namespace.

Use it for utility/helper functions that logically belong to the class but don’t need instance or class data.

Example

Python

class MathUtils: @staticmethod def is_even(number): return number % 2 == 0 @staticmethod def square_root(n): if n < 0: raise ValueError("Cannot calculate square root of negative number") return n ** 0.5 # Call on class print(MathUtils.is_even(10)) # True # Call on instance (still works) utils = MathUtils() print(utils.square_root(16)) # 4.0

When to use @staticmethod:

  • Helper functions related to the class concept

  • No need to access instance (self) or class (cls) data

  • Cleaner than putting functions outside the class

6.3 Alternative Constructors Using @classmethod

One of the most common and powerful uses of @classmethod is to create alternative constructors — ways to create objects other than the default init.

Classic example – Create object from string

Python

class Person: def init(self, name, age): self.name = name self.age = age @classmethod def from_birth_year(cls, name, birth_year): current_year = 2026 age = current_year - birth_year return cls(name, age) # calls init indirectly def str(self): return f"{self.name}, {self.age} years old" # Normal way p1 = Person("Anshuman", 25) # Alternative constructor p2 = Person.from_birth_year("Rahul", 2000) print(p2) # Rahul, 26 years old

Another real-world example – Factory from file

Python

class Config: def init(self, settings): self.settings = settings @classmethod def from_json_file(cls, filename): import json with open(filename, "r") as f: data = json.load(f) return cls(data) # Usage config = Config.from_json_file("config.json")

6.4 @property as a Decorator

@property is a built-in decorator that turns a method into a getter — so you can access it like an attribute.

Combined with @<name>.setter and @<name>.deleter, you get full control over attribute access.

Full example – Controlled access

Python

class Employee: def init(self, name, salary): self.name = name self._salary = salary # protected @property def salary(self): # getter return self._salary @salary.setter def salary(self, value): # setter if value < 0: raise ValueError("Salary cannot be negative") self._salary = value print(f"Salary updated to ₹{value}") @salary.deleter def salary(self): # deleter (rarely used) print("Salary record deleted") self._salary = 0 emp = Employee("Priya", 80000) print(emp.salary) # 80000 ← calls getter emp.salary = 95000 # calls setter → "Salary updated to ₹95000" # emp.salary = -5000 # ValueError del emp.salary # calls deleter → "Salary record deleted" print(emp.salary) # 0

Why use @property instead of get/set methods?

  • Cleaner syntax: emp.salary instead of emp.get_salary()

  • Can add logic (validation, logging, computation) later without breaking existing code

  • Makes class feel more like a simple data container

Mini Summary Project – Temperature Converter with Properties

Python

class Temperature: def init(self, celsius): self._celsius = celsius @property def celsius(self): return self._celsius @celsius.setter def celsius(self, value): self._celsius = value @property def fahrenheit(self): return (self._celsius 9/5) + 32 @fahrenheit.setter def fahrenheit(self, value): self._celsius = (value - 32) 5/9 temp = Temperature(25) print(temp.celsius) # 25 print(temp.fahrenheit) # 77.0 temp.fahrenheit = 98.6 print(temp.celsius) # 37.0

This completes the full Class Methods, Static Methods and Decorators section — now you know how to write flexible, reusable, and clean class-level code in Python

📚 Amazon Book Library

All my books are FREE on Amazon Kindle Unlimited🌍 Exclusive Country-Wise Amazon Book Library – Only Here!

On GlobalCodeMaster.com you’ll find complete, ready-to-use lists of my books with direct Amazon links for every country.
Belong to India, Australia, USA, UK, Canada or any other country? Just click your country’s link and enjoy:
Any eBook FREE on Kindle Unlimited ✅ Or buy at incredibly low prices
400+ fresh books written in 2025-2026 with today’s latest AI, Python, Machine Learning & tech trends – nowhere else will you find this complete country-wise collection on one platform!
Choose your country below and start reading instantly 🚀
BOOK LIBRARY USA 2026 LINK
BOOK LIBRARY INDIA 2026 LINK
BOOK LIBRARY AUSTRALIA 2026 LINK
BOOK LIBRARY CANADA 2026 LINK
BOOK LIBRARY UNITED KINGDOM 2026 LINK
BOOK LIBRARY GERMANY 2026 LINK
BOOK LIBRARY FRANCE 2026 LINK
BOOK LIBRARY ITALY 2026 LINK
BOOK LIBRARY SPAIN 2026 LINK
BOOK LIBRARY NETHERLANDS 2026 LINK
BOOK LIBRARY BRAZIL 2026 LINK
BOOK LIBRARY MEXICO 2026 LINK
BOOK LIBRARY JAPAN 2026 LINK
BOOK LIBRARY POLAND 2026 LINK
BOOK LIBRARY IRELAND 2026 LINK
BOOK LIBRARY SWEDEN 2026 LINK
BOOK LIBRARY BELGIUM 2026 LINK