Classes & Objects
Classes are blueprints for creating objects. Objects are instances of classes with their own data (attributes) and behavior (methods).
20 min•By Priygop Team•Updated 2026
Classes & Objects
Classes & Objects
# Define a class
class Dog:
# Class attribute (shared by all instances)
species = "Canis familiaris"
# Constructor (__init__)
def __init__(self, name, age):
# Instance attributes (unique to each object)
self.name = name
self.age = age
# Instance method
def bark(self):
return f"{self.name} says Woof!"
def info(self):
return f"{self.name} is {self.age} years old"
# Create objects (instances)
dog1 = Dog("Rex", 5)
dog2 = Dog("Buddy", 3)
print(dog1.bark()) # Rex says Woof!
print(dog2.info()) # Buddy is 3 years old
print(dog1.species) # Canis familiaris
# Modify attributes
dog1.age = 6
print(f"{dog1.name} is now {dog1.age}")
# isinstance check
print(isinstance(dog1, Dog)) # TrueTip
Tip
Think of classes as blueprints and objects as houses built from them. Each house (object) has its own paint color (attributes) but same structure.
Diagram
Loading diagram…
Classes are syntactic sugar over prototypes.
Common Mistake
Warning
Forgetting self as the first parameter in methods. def bark(): causes TypeError. It must be def bark(self):.
Practice Task
Note
(1) Create a Car class with make, model, year. (2) Add a describe() method. (3) Create 3 car objects and print their info.