Python OOP Cheat Sheet
Object-oriented Python covering classes, inheritance, dunder methods, computed properties, class/static methods, and abstract base classes.
2 PagesIntermediateMar 30, 2026
Classes & Instances
Defining a class with instance and class attributes.
python
class Dog: species = "Canis familiaris" # class attribute, shared def __init__(self, name, age): self.name = name # instance attribute self.age = age def bark(self): return f"{self.name} says Woof!"d = Dog("Rex", 3)d.bark() # 'Rex says Woof!'
Inheritance & super()
Extending classes and calling parent implementations.
python
class Animal: def __init__(self, name): self.name = name def speak(self): raise NotImplementedErrorclass Cat(Animal): def speak(self): return f"{self.name} says Meow"class Kitten(Cat): def speak(self): return super().speak() + " (softly)"
Common Dunder Methods
Special methods that customize built-in behavior.
- __init__(self, ...)- called when an instance is created, sets up attributes
- __repr__(self)- unambiguous developer-facing string representation
- __str__(self)- readable user-facing string, used by print() and str()
- __eq__(self, other)- defines behavior for the == operator
- __len__(self)- defines behavior for len(obj)
- __add__(self, other)- defines behavior for the + operator
- __iter__(self)- makes an object iterable with for loops
- __enter__ / __exit__- implement the context manager protocol for with statements
Properties, classmethod & staticmethod
Computed attributes and alternative constructors.
python
class Circle: def __init__(self, radius): self._radius = radius @property def area(self): return 3.14159 * self._radius ** 2 @classmethod def unit_circle(cls): return cls(1) # receives the class, not an instance @staticmethod def is_valid_radius(r): return r > 0 # no access to self or clsc = Circle(2)c.area # computed on access, no parentheses needed
Abstract Base Classes
Enforcing that subclasses implement required methods.
python
from abc import ABC, abstractmethodclass Shape(ABC): @abstractmethod def area(self): ...class Square(Shape): def __init__(self, side): self.side = side def area(self): return self.side ** 2# Shape() # raises TypeError: Can't instantiate abstract class
Pro Tip
Implement `__repr__` on every class you write, even quick ones — it makes debugging and REPL sessions vastly easier, and if `__str__` is missing Python falls back to `__repr__` automatically.
Was this cheat sheet helpful?
Explore Topics
#PythonOOP#PythonOOPCheatSheet#Programming#Intermediate#ClassesInstances#InheritanceSuper#CommonDunderMethods#PropertiesClassmethodStaticmethod#OOP#Functions#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance