All Courses
Object-Oriented Programming

Creating Classes in Python

Creating a Class

Use the class keyword followed by the class name and a colon.

class Person:
    pass

Naming Convention — PascalCase: Capitalize the first letter of every word. - ✅ Person, BigCar, FruitBasket - ❌ person, big_car, person_one

Even an empty class creates a new data type. You can already instantiate it:

p1 = Person()
print(type(p1))  # <class '__main__.Person'>

The __init__ Method (Constructor)

__init__ is a dunder method (double underscore) that runs automatically whenever a new instance is created. It's used to initialize the object with data.

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
  • You never call __init__ manually — Python calls it for you
  • Parameters defined here (except self) must be passed when creating an instance
p1 = Person("Tim", 21)
p2 = Person("Bill", 40)

The self Keyword

self refers to the specific instance the method is acting on. It must be the first parameter in every method.

  • Think of self as being the instance itself (e.g. p1 or p2)
  • self.name = name stores name as an attribute on that instance
  • You don't pass self manually — Python passes it automatically
# Inside the class:
self.name = name   # Same as p1.name = name (from outside)

Attributes

An attribute is data stored on a specific instance of a class. Each instance has its own copy.

print(p1.name)  # "Tim"
print(p1.age)   # 21

print(p2.name)  # "Bill"
print(p2.age)   # 40

Modifying / Adding Attributes Outside the Class

You can access, change, or even add new attributes from outside the class:

p1.name = "Random"      # Modify existing attribute
p1.nickname = "Timmy"   # Create a brand new attribute

⚠️ Guide: Modifying attributes from outside the class is generally not recommended. Only do it when necessary.


Full Example — Fruit Class

class Fruit:
    def __init__(self, name, calories):
        self.name = name
        self.calories = calories

# Create an instance
apple = Fruit("Apple", 100)

# Add an attribute outside the class
apple.color = "Red"

# Access attributes
print(apple.name)      # Apple
print(apple.calories)  # 100
print(apple.color)     # Red

Why Use Classes?

Classes encapsulate (group together) common behavior shared by all instances of a type.

  • All strings share the same methods (.upper(), .lower(), etc.)
  • All lists share the same methods (.append(), .index(), etc.)
  • Even though each instance holds different data, they all behave the same way

Encapsulation = taking related data and behavior and grouping it together in one place (a class).


Terminology Recap

Term Meaning
class Keyword to define a new class (data type)
__init__ Special method that runs on instance creation
self Reference to the current instance inside a method
Attribute Data stored on a specific instance (self.name)
Dunder method A method with double underscores (__init__, etc.)
PascalCase Naming convention for classes (MyClassName)
Encapsulation Grouping related behavior/data together in a class