🌟 Object-Oriented Programming (OOP) in Python – Quick Student Guide
1. Introduction to OOPs
OOP stands for Object-Oriented Programming.
It organizes code into objects (real-world entities).
Helps in reusability, modularity, and scalability.
Example: A Car class can represent multiple car objects like car1, car2 with different colors
but same structure.
2. Attributes & Methods
Attributes = variables inside a class (data/properties).
Methods = functions inside a class (behaviors).
class Car:
def __init__(self, brand, color):
[Link] = brand # Attribute
[Link] = color # Attribute
def drive(self): # Method
print(f"{[Link]} is driving!")
3. Characteristics of OOP
Encapsulation → wrapping data & methods together (protects internal details).
Inheritance → one class inherits from another.
Polymorphism → same method name works differently depending on object.
Abstraction → hiding implementation, showing only essentials.
4. Creating a Class & Instantiation
class Student:
def __init__(self, name):
[Link] = name
# Instantiation (creating an object)
s1 = Student("Alice")
print([Link])
5. Constructor Method
Special method __init__ is called automatically when an object is created.
class Dog:
def __init__(self, name):
[Link] = name
6. Class Attributes vs Object Attributes
Class attribute: Shared by all objects of the class.
Object attribute: Unique to each object.
class Student:
college = "ABC University" # Class attribute
def __init__(self, name):
[Link] = name # Object attribute
7. What does self mean?
Refers to the current object instance.
Needed to access object attributes and methods inside the class.
8. Built-in Methods like hasattr, etc.
hasattr(obj, "attr") → check if an attribute exists.
getattr(obj, "attr", default) → get attribute value.
setattr(obj, "attr", value) → set attribute.
delattr(obj, "attr") → delete attribute.
9. Inheritance
Reuse code by deriving new classes from existing ones.
class Animal:
def speak(self):
print("I make sounds")
class Dog(Animal):
def speak(self):
print("Woof!")
10. Polymorphism
Same method name, but different behaviors.
for animal in [Dog(), Animal()]:
[Link]()
11. Abstract Classes
Classes that cannot be instantiated.
Use abc module.
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
12. Class Composition
A class contains another class object (has-a relationship).
class Engine:
def start(self):
print("Engine starts")
class Car:
def __init__(self):
[Link] = Engine() # Composition
13. Private & Strongly Private
_attr → weakly private (convention: “don’t touch”).
__attr → strongly private (name mangled).
class Test:
def __init__(self):
self._temp = 10 # Private by convention
self.__secret = 99 # Strongly private
14. Special Methods
Built-in dunder methods (__xxx__).
Examples:
__str__ → string representation.
__len__ → define length.
__add__ → operator overloading.
class Book:
def __init__(self, title, pages):
[Link] = title
[Link] = pages
def __str__(self):
return f"Book: {[Link]}"
def __len__(self):
return [Link]