Back to Study Material Python

OOP Concepts

Object-Oriented Programming rests on a small set of foundational concepts that shape how you design and write Python programs. Understanding these concepts...

OOP Concepts

Core OOP Concepts Every Python Developer Should Know

Object-Oriented Programming rests on a small set of foundational concepts that shape how you design and write Python programs. Understanding these concepts deeply allows you to make better decisions when building systems of any size.

The first concept is the class, which serves as a blueprint or template. A class defines what attributes an object will have and what methods it can perform. From a single class, you can create many independent objects, each with its own data.

The second concept is the object or instance. When you call a class like a function, Python creates a new object in memory. This object carries its own copy of the instance attributes defined in the class's __init__ method. Two objects from the same class share method definitions but have separate attribute values.

The third concept is the method. Methods are functions defined inside a class. They always receive the instance as their first argument, conventionally named self. Through self, methods can read and modify the object's own data.

Attributes come in two varieties. Instance attributes belong to individual objects and are typically set in __init__. Class attributes belong to the class itself and are shared by all instances unless overridden on a particular object.

class BankAccount: interest_rate = 0.03 # class attribute

def __init__(self, owner, balance): self.owner = owner # instance attribute self.balance = balance # instance attribute

def deposit(self, amount): self.balance += amount

account1 = BankAccount("Alice", 1000) account2 = BankAccount("Bob", 500)

Here interest_rate is shared, while owner and balance differ per object. Calling account1.deposit(200) only changes account1's balance, not account2's.

Exam tip: Python resolves attribute lookups first on the instance, then on the class. If you assign a value to an attribute on an instance that shares a name with a class attribute, Python creates a new instance attribute that shadows the class one. This behavior surprises many beginners and frequently appears in exam questions.