A class is a blueprint for creating objects. It bundles together data (attributes) and the behaviour that operates on that data (methods). Object-oriented programming lets you model real-world things — a student, an account, an order — as your own custom types.
Anatomy of a class
class Student:def __init__(self, name, package): # the constructorself.name = name # instance attributesself.package = packagedef describe(self): # a methodreturn f"{self.name}: ₹{self.package} LPA"s = Student("Aanya", 7.5) # create an instances.name # 'Aanya' — access an attributes.describe() # 'Aanya: ₹7.5 LPA' — call a method
self and __init__
__init__ runs automatically when you create an instance, setting up its starting attributes. self is the instance the method is working on — every method takes it as its first parameter, and you use it to read/write that object's attributes.
Key takeaways
- 01A class bundles data (attributes) and behaviour (methods) into a custom type.
- 02__init__ is the constructor — it initialises each new instance.
- 03self refers to the current instance; every method takes it first.
- 04Create an instance by calling the class: s = Student(...).
- 05Class names use PascalCase by convention.
