Classes, Instances and the self Parameter
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Everything in Python is an object: numbers, strings, lists, functions, and modules. In Object-Oriented Programming (OOP), a class is a blueprint that bundles data (attributes) and behavior (methods) into reusable objects.
Defining a Class and Creating Instances
Class is a blueprint that describes what data and behaviour its objects will have. Writing the class creates nothing yet; calling it makes an object.
Use the class keyword followed by the class name in PascalCase (e.g. BankAccount, UserProfile).
class Car: def drive(self): return "The car is driving on the highway."
# Create an instancemy_car = Car()print(my_car.drive())Example 1 · A class with two methods
class Invoice: def describe(self): return "Invoice for a customer order"
def with_tax(self, amount): return amount + amount * 18 // 100
inv = Invoice()print(inv.describe())print(inv.with_tax(1000))Invoice for a customer order 1180
What this code does
- The class lists the behaviour once.
Invoice()makes one object that can use it. selfis filled in by Python, so the call passes only1000.
Car() instantiates a new object in memory. my_car holds a reference to that instance.
Why self is Explicit in Python
self is the first parameter of an instance method. It receives the object the method was called on, so the method can read and change that object.
Unlike languages like Java or C++ where this is implicit and hidden, Python makes self explicit as the first parameter of every instance method.
class Dog: def bark(self): print(f"Dog instance at memory address {id(self)} says Woof!")Example 2 · A method that takes an argument
class Drone: def report_status(self, battery_level): return f"Drone active. Battery: {battery_level}%"
d = Drone()print(d.report_status(85))Drone active. Battery: 85%
What this code does
- The
Droneclass defines thereport_statusmethod. selfautomatically binds todwhen called asd.report_status(85).- The method formats and returns the string.
Example 3 · A method call is a function call in disguise
class Ticket: def label(self): return f"Ticket #{self.number}"
t = Ticket()t.number = 7print(t.label())print(Ticket.label(t))Ticket #7 Ticket #7
What this code does
t.label()andTicket.label(t)give the same result.- The first form is what you normally write. Python turns it into the second.
When you call dog.bark(), Python automatically converts it behind the scenes to Dog.bark(dog). self represents the specific instance invoking the method.
Object Identity and the id() Function
Object identity is the single, unchanging id every object has while it exists. Two names with the same identity are two names for one object.
Every object created in Python has a unique integer identity corresponding to its memory location during its lifetime.
car1 = Car()car2 = Car()print(id(car1) != id(car2)) # Trueprint(car1 is car2) # FalseExample 4 · Two objects, one object
class Customer: pass
a = Customer()b = Customer()c = aa.name = "Meera"b.name = "Meera"print(a is b)print(a is c)print(a.name == b.name)False True True
What this code does
aandbare separate objects, soa is bisFalseeven with equal names.c = acopies the reference, not the object, soa is cisTrue.
is checks whether two variables point to the exact same object identity in memory.
Dynamic Attribute Assignment
Because Python is dynamic, you can attach attributes directly to an instance at runtime.
car1.color = "Red"print(car1.color)Example 5 · Attributes belong to one object
class Account: pass
x = Account()y = Account()x.balance = 500print(x.balance)try: print(y.balance)except AttributeError: print("y has no balance")500 y has no balance
What this code does
- Setting
x.balanceadds the attribute toxonly. ywas never given one, so reading it raisesAttributeError.
While dynamic attributes are possible, defining your attributes systematically inside the constructor (__init__) is the standard practice for clean, predictable architecture.
Next step
Check your understanding
12 quick questions