# Classes: Blueprints for Your Own Things

A class describes one kind of thing your program keeps track of, what it holds and what it can do, so every object made from it gets the same attributes, the same methods and a friendly printout.

Apprentice · Python · OOP · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Write a class with \_\_init\_\_ and self, and make objects that each hold their own attributes
- Give a class methods that use and change its attributes, and a \_\_str\_\_ method that print() uses

## Key takeaways

- A class is a blueprint for one kind of thing, and each object made from it holds its own values.
- __init__ (read "dunder init") runs when an object is made and gives it its attributes, such as self.name.
- self is the object a method is working on, so self.name is that object's own name.
- A method is a function that belongs to a class, called on an object with a dot, as in cactus.water(today).
- __str__ decides what print() shows for an object. Without it, Python prints the class's name and a memory address.

Every program that keeps track of things, such as students, orders or songs, needs a way to say what one of those things is: what it holds and what it can do. A class is how Python says it. You'll write classes for your own programs, and nearly every library you use hands you objects made from classes, from a web framework's incoming request to a test tool's results.

The example is Leaf & Sprout, a houseplant app that reminds you when each plant needs water.

## A pile of dictionaries that drift

A dictionary can hold one plant's details, so a first try keeps each plant as a dictionary:

```python
fern = {"name": "Fern", "water_every": 3, "last_watered": "May 8"}
cactus = {"name": "Cactus", "water_every": 14}
pothos = {"nmae": "Pothos", "water_every": 7, "last_watered": "May 1"}

for plant in [fern, cactus, pothos]:
    print(plant["name"], "needs water every", plant["water_every"], "days")
```

```text
Fern needs water every 3 days
Cactus needs water every 14 days
Traceback (most recent call last):
  File "C:\Users\Sam\python-course\plants.py", line 6, in <module>
    print(plant["name"], "needs water every", plant["water_every"], "days")
          ~~~~~^^^^^^^^
KeyError: 'name'
```

Two plants print, then the program stops: the third dictionary says `nmae` where `name` belongs. Nothing checked it when the pothos was made, so the typo waited until a loop, somewhere else, read the key. The cactus has a problem waiting too: it has no `last_watered` at all. Every line that makes a plant has to remember the same keys, spelled the same way, and nothing says what a plant is.

## A class is a cookie cutter

A cookie cutter gives every cookie the same shape, and each cookie still gets its own sprinkles. A **class** is the cutter: it describes one kind of thing, once. Each **object** made from it, also called an **instance**, is a cookie with its own values. In your textbook, building programs from classes and objects is called object-oriented programming.

```python
class Plant:
    def __init__(self, name, water_every):
        self.name = name
        self.water_every = water_every


fern = Plant("Fern", 3)
cactus = Plant("Cactus", 14)
print(fern.name, "needs water every", fern.water_every, "days")
print(cactus.name, "needs water every", cactus.water_every, "days")
```

```text
Fern needs water every 3 days
Cactus needs water every 14 days
```

- `class Plant:` starts the class. Class names are written in CapitalizedWords (`Plant`, `CareLog`), so they stand out from variables.
- `__init__` (read "dunder init", short for the double underscores on each side) is a method Python runs every time an object is made. `Plant("Fern", 3)` makes a new, empty plant and calls `__init__` with it, `"Fern"` and `3`.
- `self` is that new object. `self.name = name` stores the name on it. A value stored on an object is an **attribute**, and you read it with a dot: `fern.name`.

![A class as a cookie cutter: one cutter labeled class Plant stamps out two cookies of the same shape, the objects fern and cactus, and each cookie carries its own values, name Fern and water_every 3 on one, name Cactus and water_every 14 on the other](https://fsdtrainingmedia.blob.core.windows.net/public/images/2026/11/612c8acc41984c4a8cd0dda422e24e48.webp)

Now a plant gets its values in one place, and mistakes stop where they're made. `Plant("Pothos")` stops on that line with `TypeError: Plant.__init__() missing 1 required positional argument: 'water_every'`, and `fern.nmae` stops with `AttributeError: 'Plant' object has no attribute 'nmae'`.

## Methods: what a plant can do

A function that belongs to a class is a **method**. Its first parameter is `self`, the object it's called on, so a method can read and change that object's attributes. Leaf & Sprout's plants need two: `water()` records the day, and `needs_water()` says whether enough days have passed.

The dates come from Python's `datetime` module: `date(2026, 5, 8)` is May 8, 2026, and subtracting one date from another gives the gap between them, whose `.days` is a whole number of days.

```python
from datetime import date


class Plant:
    def __init__(self, name, water_every, last_watered):
        self.name = name
        self.water_every = water_every
        self.last_watered = last_watered

    def water(self, today):
        self.last_watered = today

    def needs_water(self, today):
        days = (today - self.last_watered).days
        return days >= self.water_every


fern = Plant("Fern", 3, date(2026, 5, 8))
cactus = Plant("Cactus", 14, date(2026, 4, 20))
today = date(2026, 5, 10)
print("Fern:", fern.needs_water(today))
print("Cactus:", cactus.needs_water(today))
cactus.water(today)
print("Cactus after watering:", cactus.needs_water(today))
```

```text
Fern: False
Cactus: True
Cactus after watering: False
```

You call a method on an object with a dot: `cactus.water(today)`. Python fills in `self` for you, so the call passes one argument while the `def` line lists two. Watering the cactus changed only the cactus's `last_watered`. The fern's stayed as it was, because each object keeps its own attributes.

## A friendly printout with \_\_str\_\_

Print a plant as it is, and Python shows its class and where it sits in the computer's memory, something like `<__main__.Plant object at 0x7e1eabd30d70>`. That helps nobody. A method named `__str__` (read "dunder str") decides what `print()` shows: it returns the text.

```python
from datetime import date


class Plant:
    def __init__(self, name, water_every, last_watered):
        self.name = name
        self.water_every = water_every
        self.last_watered = last_watered

    def __str__(self):
        return f"{self.name}: water every {self.water_every} days, last watered {self.last_watered}"


pothos = Plant("Pothos", 7, date(2026, 5, 1))
print(pothos)
```

```text
Pothos: water every 7 days, last watered 2026-05-01
```

`print(pothos)` calls `pothos.__str__()` for you and prints what comes back, and an f-string such as `f"Today: {pothos}"` uses it too. Methods with two underscores on each side of their names are *dunder methods*: you rarely call them yourself, because Python calls them at the right moment.

One thing still shows the memory form: a list of plants, such as `print([pothos])`. A list shows each item with a second dunder method, `__repr__` (read "dunder repr"), the developer's view of an object. Give a class a `__repr__` too when you'll print lists of its objects.

## Do it: write a class for seedlings

Leaf & Sprout also tracks seedlings growing on a windowsill, until each one is tall enough to move to a bigger pot.

- `name`: what the seedling is, such as `"Basil"`.
- `height`: how tall it is, in centimeters, when the app starts tracking it.

**Your job:** write the class `Seedling`.

- `__init__(self, name, height)` stores both on the new object, as `self.name` and `self.height`.
- The method `grow(self, cm)` adds `cm` to the seedling's height.
- The method `ready_to_repot(self)` returns `True` when the height is 10 or more, and `False` when it's less.
- `__str__` returns the name, the word `seedling`, a comma and the height in `cm`, such as `Basil seedling, 4 cm`.
- Each seedling has its own height: growing one doesn't change another.

Worked through: `basil = Seedling("Basil", 4)` makes a seedling 4 cm tall, so `str(basil)` is `"Basil seedling, 4 cm"` and `basil.ready_to_repot()` is `False`. After `basil.grow(7)`, its height is 4 + 7 = 11, so `basil.ready_to_repot()` is `True` and `str(basil)` is `"Basil seedling, 11 cm"`.

Practiced live on the lesson page: write the Python right there and check it.

## Check yourself

2 questions to try on the lesson page.

## So what

A class says once what a thing is and what it can do, so every object your program makes has the right shape from the line that creates it.

## Key terms

- [Class](https://www.ask-codey.com/glossary/class)
- [\_\_init\_\_ (Constructor)](https://www.ask-codey.com/glossary/init-constructor)
- [self](https://www.ask-codey.com/glossary/self)
- [Attribute](https://www.ask-codey.com/glossary/attribute)
- [Method](https://www.ask-codey.com/glossary/method)
- [\_\_str\_\_ and \_\_repr\_\_](https://www.ask-codey.com/glossary/str-and-repr)

## Related

- [Dataclasses: Classes That Hold Data](https://www.ask-codey.com/lessons/python-dataclasses)
- [Inheritance: One Class Built on Another](https://www.ask-codey.com/lessons/python-classes-inheritance)

Source: https://www.ask-codey.com/lessons/python-classes-basics
