Python Classes and Objects Foundation
class / instance OOP 01 Foundation

Classes are blueprints; objects are instances

Understand the distinction between a class (the template) and objects (the concrete things created from that template).

Picture this: many objects, one class

Each object has its own attributes. Methods use self to mean this object.

classshared recipe
→
obj1own data
→
obj2own data
→
selfthis object

Diagram — class to instances

class object A object B

Class vs Object

Class: A blueprint or template that defines what properties and methods an object can have. Written once in code.
Object: A concrete instance created from that class. You can create many objects from one class.

UML Class Diagram

classDiagram
    class Car {
      -color: str
      +__init__(color)
      +drive()
    }
    Car : drive()
    Car : color
    car1 : Car
    car2 : Car
    Car <|-- car1
    Car <|-- car2
            
A class is a blueprint; objects (car1, car2) are instances created from it.

Blueprint vs Instance

Class Car car1 car2
The class (blueprint) creates objects (instances).

Real-world analogy

A class is like a cookie cutter — the template. Each cookie you make with that cutter is an object — a unique instance. All cookies have the same shape, but each one is independent.

Class defines

  • Attributes (properties/data)
  • Methods (functions/behavior)
  • Constructor (__init__)

Objects have

  • Unique state (own values)
  • Own identity (separate in memory)
  • Can call methods independently

Class blueprint

1class Car: # Template
2def __init__(self, color):
3self.color = color
4def drive(self):
5print(f"{self.color} car driving")

Creating multiple objects from one class

Press "Run one step" to see class instantiation

Example Program

1car1 = Car("red") # Object 1
2car2 = Car("blue") # Object 2
3car1.drive() # red car driving
4car2.drive() # blue car driving