← Back to Blog

Classes and Objects: The Blueprint and the Thing

The idea

A class is a blueprint. An object is what you build from it.

The blueprint for a house defines how many rooms it has, where the door is, what materials it uses. The house itself — the one you can walk into — is the object. You can build a hundred houses from the same blueprint. Each one is independent. Changing one doesn't affect the others.

That's the relationship between a class and an object in Python.

Defining a class

class Animal:
    pass

That's a class. Empty — but valid. class is the keyword. Animal is the name. Convention: class names use CapitalizedWords — not underscores, not lowercase.

Creating an object

lassie = Animal()

That's an object. Animal() calls the class and creates an instance of it. lassie is the variable that holds it.

You can create as many as you want:

lassie   = Animal()
whiskers = Animal()
rex      = Animal()

Three objects. One blueprint. Each one independent.

The constructor — __init__

Right now every Animal object is identical and empty. To give each one its own data at creation, we use __init__ — the constructor.

class Animal:
    def __init__(self, name, species, age):
        self.name    = name
        self.species = species
        self.age     = age

__init__ runs automatically every time you create a new object. The arguments you pass at creation go directly into it.

lassie = Animal("Lassie", "dog", 4)
print(lassie.name)      # Lassie
print(lassie.species)   # dog
print(lassie.age)       # 4

Each object has its own data. lassie.name is "Lassie". A second object would have its own name, its own species, its own age — completely separate.

What self is

self refers to the object being created or used. When you write self.name = name, you're saying: "store this value on this specific object". Not on the class. Not globally. On this one.

self gets its own article — it deserves one. For now, know that it's always the first parameter in any method inside a class, and Python passes it automatically.

Adding a method

A method is a function that lives inside a class. It operates on the object's own data.

class Animal:
    def __init__(self, name, species, age):
        self.name    = name
        self.species = species
        self.age     = age

    def describe(self):
        print(f"{self.name} is a {self.age} year old {self.species}.")
lassie = Animal("Lassie", "dog", 4)
lassie.describe()

Output → Lassie is a 4 year old dog.

The method knows about self.name, self.age, and self.species because they belong to the same object. No arguments needed — the data is already there.

Multiple objects, same class

lassie   = Animal("Lassie", "dog", 4)
whiskers = Animal("Whiskers", "cat", 2)

lassie.describe()
whiskers.describe()

Output:

Lassie is a 4 year old dog.
Whiskers is a 2 year old cat.

Same blueprint. Two objects. Each one knows its own data.

Heads up!

  • Class names use CapitalizedWords — Animal, CalorieTracker, not animal or calorie_tracker
  • __init__ runs automatically at creation — you never call it directly
  • self is always the first parameter in a method — Python passes it automatically
  • An object's data is accessed with dot notation — lassie.name, not lassie["name"]

The mindset shift

Stop thinking: "I have a dictionary and some functions."

Start thinking: "I have a thing. It knows its own data. It knows what it can do."

What you should understand now

  • A class is a blueprint — defined once, used many times
  • An object is an instance of a class — created from the blueprint
  • __init__ is the constructor — it runs at creation and sets the object's data
  • self refers to the specific object being used
  • Methods are functions that live inside a class and operate on the object's data
[ login to bookmark ] // copied! 34 views · 3 min
// resources
Code Example oop_classes_objects.py
← prev When Functions Aren't Enough next → What an Object Knows and What It Can Do
// 0 comments
// No comments yet. Be the first.
// leave a comment

// Your comment will appear after approval.