Dunder Methods

__str__, __repr__, __len__, __getitem__ — make classes feel built-in

30 min · interactive lesson with runnable code and 5 graded exercises.

What you'll be able to do

  • Explain how Python syntax maps to dunder methods
  • Write a good __repr__ (and know when __str__ is used)
  • Implement __len__ and __getitem__, including slices
  • Build a minimal Dataset class

The full lesson is free with an account.

What is a dunder method?

__str__ vs __repr__

__len__

__getitem__

A tiny Dataset

Common pitfalls