Object-Oriented Programming
Classes and Objects
Defining classes and creating objects
Interview: Core OOP concept — frequently asked in interviews to test understanding of class vs instance, memory model, and object lifecycle
Object-Oriented Programming (OOP) is a programming paradigm that organizes code around objects — data structures that bundle attributes (data) and methods (behavior). Python is inherently object-oriented: everything in Python is an object, including integers, strings, and functions.
Classes as Blueprints
A class is a blueprint or template for creating objects. It defines the initial state (attributes) and behavior (methods) that objects of that class will have. Think of a class as an architectural blueprint and objects as the actual houses built from it.
- Class: A template defining attributes and methods (e.g.,
class Dog) - Object (Instance): A specific realization of a class (e.g.,
my_dog = Dog("Buddy")) - Instantiation: The process of creating an object from a class
- Attributes: Variables that belong to the class or instance
- Methods: Functions defined inside a class that describe the behavior of objects
Defining a Class
Use the class keyword followed by the class name (conventionally in PascalCase). The __init__ method is the constructor that initializes new objects. The self parameter refers to the instance being created.
Creating and Using Objects
Objects are created by calling the class like a function. Each object has its own independent state. You access attributes and methods using dot notation (object.attribute).
Class vs Instance — Key Differences
- Class attributes are shared by all instances — defined at class level
- Instance attributes are unique to each object — defined in
__init__ - Modifying a class attribute affects all instances (unless shadowed)
- Each object maintains its own independent state through instance attributes
Interview Tip
Interviewers often ask about the difference between type(obj) and obj.__class__. They return the same thing for regular objects, but differ for old-style classes in Python 2 and some proxy objects. In modern Python 3, they are identical.
Common Pitfall
Forgetting self as the first parameter in methods is the most common beginner mistake. Python automatically passes the instance as the first argument — if you omit self, you'll get a TypeError.
Use Cases
Modeling real-world entities (User, Product, Order) in applications
Creating GUI components (Button, Window, Dialog)
Building game objects (Player, Enemy, Projectile)
Data transfer objects (DTOs) for API communication
Implementing design patterns (Singleton, Factory, Observer)
Common Mistakes
Forgetting self as the first parameter in instance methods
Confusing class attributes with instance attributes — modifying class attrs through instance creates shadowing
Using mutable default arguments in __init__ (e.g., def __init__(self, items=[]))
Calling a method on the class instead of the instance (Dog.bark() vs dog1.bark())
Not understanding that everything in Python is an object — including classes themselves