Skip to main content
Albero Academy

Classes

Define your own types that bundle data and behaviour together.

Object-Oriented Programming12 min

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

a class
class Student:
def __init__(self, name, package): # the constructor
self.name = name # instance attributes
self.package = package
def describe(self): # a method
return f"{self.name}: ₹{self.package} LPA"
s = Student("Aanya", 7.5) # create an instance
s.name # 'Aanya' — access an attribute
s.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

  1. 01A class bundles data (attributes) and behaviour (methods) into a custom type.
  2. 02__init__ is the constructor — it initialises each new instance.
  3. 03self refers to the current instance; every method takes it first.
  4. 04Create an instance by calling the class: s = Student(...).
  5. 05Class names use PascalCase by convention.

Finished this chapter?

Mark it complete to track your progress through Python.