Python course ยท Module 3: Object-Oriented Programming

Magic methods - hidden animal behaviors

13 min read ยท
In this lesson11

Welcome back! Darwin here with a fascinating lesson about Python's hidden powers.

In nature, animals have hidden behaviors - a lion roars, an elephant trumpets, a chameleon camouflages. In Python, classes can also have hidden behaviors through magic methods - special methods with double underscores!

lion = Animal("Lion")
print(lion)  # How does Python know what to display? __str__!
len(lion)    # How does Python know what to measure? __len__!
lion + elephant  # How does Python know what to add? __add__!

What are magic methods?

Magic methods (also called dunder methods from "double underscore") are special methods with names in the format __method__ that Python automatically calls in specific situations.

Safari Analogy: They're like natural instincts of animals - you don't have to teach them, they're built into their nature!

class Animal:
    def __init__(self, name):
        """Called when CREATING an object"""
        self.name = name

    def __str__(self):
        """Called by print() and str()"""
        return f"Animal: {self.name}"

    def __len__(self):
        """Called by len()"""
        return len(self.name)

lion = Animal("Lion")  # Calls __init__
print(lion)           # Calls __str__
print(len(lion))      # Calls __len__

Why "magic"?

  • Python calls them automatically
  • They make your classes behave like built-in types
  • They allow using standard Python syntax

Basic magic methods

1. __init__ - Constructor

You already know this one! Called when creating an object.

class Species:
    def __init__(self, name, population):
        """Initialization - you already know this!"""
        self.name = name
        self.population = population

lion = Species("Lion", 500)  # Calls __init__

2. __str__ - Human-readable representation

Called by print() and str() - returns a user-friendly string. When you write print(lion), print() first converts the argument to text with str(lion), and str() calls the method lion.__str__().

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

    def __str__(self):
        """For humans - readable"""
        return f"{self.name} (age: {self.age} years)"

lion = Animal("Simba", 5)
print(lion)  # "Simba (age: 5 years)" - calls __str__
print(str(lion))  # Same thing

3. __repr__ - Programmer representation

Called by repr() and in the console - returns an unambiguous object representation.

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

    def __repr__(self):
        """For programmers - precise"""
        return f"Animal(name='{self.name}', age={self.age})"

    def __str__(self):
        """For users - readable"""
        return f"{self.name} ({self.age} years)"

lion = Animal("Simba", 5)

# In Python console
>>> lion
Animal(name='Simba', age=5)  # Calls __repr__

# In print
print(lion)  # "Simba (5 years)" - calls __str__

# repr() explicitly
print(repr(lion))  # "Animal(name='Simba', age=5)"

Rule: __repr__ should return code that can recreate the object!

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return f"Point({self.x}, {self.y})"

p = Point(3, 4)
print(repr(p))  # "Point(3, 4)" - you can copy and paste!

Comparison methods

Allow comparing objects with ==, <, >, etc.

class Animal:
    def __init__(self, name, weight):
        self.name = name
        self.weight = weight  # kg

    def __eq__(self, other):
        """Equality: =="""
        if not isinstance(other, Animal):
            return False
        return self.weight == other.weight

    def __lt__(self, other):
        """Less than: <"""
        if not isinstance(other, Animal):
            return NotImplemented
        return self.weight < other.weight

    def __le__(self, other):
        """Less than or equal: <="""
        return self.weight <= other.weight

    def __gt__(self, other):
        """Greater than: >"""
        return self.weight > other.weight

    def __ge__(self, other):
        """Greater than or equal: >="""
        return self.weight >= other.weight

    def __ne__(self, other):
        """Not equal: !="""
        return not self.__eq__(other)

lion = Animal("Lion", 190)
elephant = Animal("Elephant", 5000)
rhino = Animal("Rhino", 2300)

print(lion < elephant)  # True - calls __lt__
print(elephant > rhino)  # True - calls __gt__
print(lion == Animal("Tiger", 190))  # True - same weight

Tip: You can use the @functools.total_ordering decorator - define only __eq__ and one of __lt__, __le__, __gt__, __ge__, and the rest will be automatically generated!

from functools import total_ordering

@total_ordering
class Animal:
    def __init__(self, name, weight):
        self.name = name
        self.weight = weight

    def __eq__(self, other):
        """Only this and __lt__ - the rest automatically!"""
        if not isinstance(other, Animal):
            return False
        return self.weight == other.weight

    def __lt__(self, other):
        """Only this and __eq__ - the rest automatically!"""
        if not isinstance(other, Animal):
            return NotImplemented
        return self.weight < other.weight

    # __le__, __gt__, __ge__, __ne__ - automatically generated!

Arithmetic methods

Allow using mathematical operators with objects!

class Population:
    """Represents a species population"""

    def __init__(self, species, count):
        self.species = species
        self.count = count

    def __add__(self, other):
        """Addition: +"""
        if isinstance(other, Population):
            if self.species != other.species:
                raise ValueError("Different species!")
            return Population(self.species, self.count + other.count)
        elif isinstance(other, int):
            return Population(self.species, self.count + other)
        return NotImplemented

    def __sub__(self, other):
        """Subtraction: -"""
        if isinstance(other, int):
            return Population(self.species, max(0, self.count - other))
        return NotImplemented

    def __mul__(self, other):
        """Multiplication: *"""
        if isinstance(other, (int, float)):
            return Population(self.species, int(self.count * other))
        return NotImplemented

    def __str__(self):
        return f"{self.species}: {self.count} individuals"

# Using operators!
lions_north = Population("Lion", 120)
lions_south = Population("Lion", 85)

# Adding populations
total_lions = lions_north + lions_south
print(total_lions)  # "Lion: 205 individuals"

# Adding a number
more_lions = lions_north + 30
print(more_lions)  # "Lion: 150 individuals"

# Subtraction (population decrease)
fewer_lions = lions_north - 20
print(fewer_lions)  # "Lion: 100 individuals"

# Multiplication (estimating growth)
projected = lions_north * 1.5  # 50% growth
print(projected)  # "Lion: 180 individuals"

Container methods

Make your objects behave like lists, dictionaries!

__len__ - Length

When a class has a __len__ method, the len() function works on its objects just like on a list. The pack returns the number of its members:

class Pack:
    """Animal pack"""

    def __init__(self, species):
        self.species = species
        self.members = []

    def add(self, name):
        self.members.append(name)

    def __len__(self):
        """Called by len()"""
        return len(self.members)

pack = Pack("Lions")
pack.add("Simba")
pack.add("Nala")
pack.add("Mufasa")

print(len(pack))  # 3 - calls __len__

__getitem__ and __setitem__ - Index access

These two methods let you read and change an object's elements with square brackets, as in a list. Along the way the habitat also gets __contains__, which handles the in operator:

class Habitat:
    """Habitat with animals"""

    def __init__(self, name):
        self.name = name
        self.animals = []

    def __getitem__(self, index):
        """Read access: habitat[0]"""
        return self.animals[index]

    def __setitem__(self, index, value):
        """Write access: habitat[0] = "Lion" """
        self.animals[index] = value

    def __len__(self):
        return len(self.animals)

    def __contains__(self, item):
        """Operator 'in': "Lion" in habitat"""
        return item in self.animals

    def append(self, animal):
        self.animals.append(animal)

savanna = Habitat("Savanna")
savanna.append("Lion")
savanna.append("Elephant")
savanna.append("Giraffe")

# Access like a list!
print(savanna[0])  # "Lion" - calls __getitem__
savanna[1] = "Rhino"  # Calls __setitem__

# Operator in
print("Lion" in savanna)  # True - calls __contains__

# len()
print(len(savanna))  # 3 - calls __len__

# Iteration (automatic through __getitem__)
for animal in savanna:
    print(animal)

__iter__ and __next__ - Iteration

A for loop first calls __iter__ and then __next__ on every step until it gets a StopIteration exception. That is how we will build a calendar of expedition days:

class Expedition:
    """Iterator over expedition days"""

    def __init__(self, days):
        self.days = days
        self.current_day = 0

    def __iter__(self):
        """Return iterator (self)"""
        self.current_day = 0
        return self

    def __next__(self):
        """Return next element"""
        if self.current_day >= self.days:
            raise StopIteration  # End of iteration

        self.current_day += 1
        return f"Day {self.current_day}"

expedition = Expedition(5)

# Iteration with for
for day in expedition:
    print(day)
# Day 1
# Day 2
# Day 3
# Day 4
# Day 5

# Or manually
exp2 = Expedition(3)
print(next(exp2))  # "Day 1"
print(next(exp2))  # "Day 2"
print(next(exp2))  # "Day 3"

A fourth call to next(exp2) would raise StopIteration. It is the same signal that makes a for loop stop on its own.

__call__ - Callable objects

Makes an object callable like a function!

class AnimalSound:
    """Callable - object like a function"""

    def __init__(self, species, sound):
        self.species = species
        self.sound = sound

    def __call__(self, times=1):
        """Call object like a function"""
        return " ".join([self.sound] * times)

lion_roar = AnimalSound("Lion", "ROAR")
elephant_trumpet = AnimalSound("Elephant", "TRUUU")

# Call like a function!
print(lion_roar())  # "ROAR" - calls __call__
print(lion_roar(3))  # "ROAR ROAR ROAR"
print(elephant_trumpet(2))  # "TRUUU TRUUU"

Context managers - __enter__ and __exit__

Allow using the with statement!

class SafariCamera:
    """Context manager for taking photos"""

    def __init__(self, location):
        self.location = location
        self.photos = []

    def __enter__(self):
        """Called when entering the 'with' block"""
        print(f"Turning on camera in {self.location}")
        return self  # Return object for use

    def __exit__(self, exc_type, exc_val, exc_tb):
        """Called when exiting the 'with' block"""
        print(f"Photos saved: {len(self.photos)}")
        print(f"Turning off camera")
        return False  # Don't suppress exceptions

    def take_photo(self, subject):
        """Take a photo"""
        self.photos.append(subject)
        print(f"  Photo: {subject}")

# Usage with 'with' - automatic __enter__ and __exit__!
with SafariCamera("Serengeti") as camera:
    camera.take_photo("Lion hunting")
    camera.take_photo("Elephant herd")
    camera.take_photo("Giraffe by a tree")

When you run it, the console shows:

Turning on camera in Serengeti
  Photo: Lion hunting
  Photo: Elephant herd
  Photo: Giraffe by a tree
Photos saved: 3
Turning off camera

We never called __enter__ or __exit__ by hand. with ran the first method at the start of the block and the second one automatically at its end, and it would do so even if an error happened inside.

Safari example - complete cataloging system

Finally, a species class that uses almost every magic method from this lesson: its objects have a readable description, compare by population, support + and -, behave like a container and can be called like a function.

from functools import total_ordering
from datetime import datetime

@total_ordering
class Species:
    """
    Species class with a full set of magic methods

    Behaves like a built-in Python type!
    """

    all_species = []  # Registry of all species

    def __init__(self, scientific_name, common_name, population, habitat):
        """Constructor"""
        self.scientific_name = scientific_name
        self.common_name = common_name
        self.population = population
        self.habitat = habitat
        self.observations = []

        Species.all_species.append(self)

    # === REPRESENTATION ===

    def __str__(self):
        """For print() - human-friendly"""
        return f"{self.common_name} ({self.population} individuals)"

    def __repr__(self):
        """For repr() - unambiguous"""
        return (f"Species(scientific_name='{self.scientific_name}', "
                f"common_name='{self.common_name}', "
                f"population={self.population}, "
                f"habitat='{self.habitat}')")

    # === COMPARISONS (only __eq__ and __lt__, rest via @total_ordering) ===

    def __eq__(self, other):
        """Equality: =="""
        if not isinstance(other, Species):
            return False
        return self.population == other.population

    def __lt__(self, other):
        """Less than: < (comparison by population)"""
        if not isinstance(other, Species):
            return NotImplemented
        return self.population < other.population

    def __hash__(self):
        """Hash from the same field as __eq__ - equal objects must have equal hashes"""
        return hash(self.population)

    # === ARITHMETIC ===

    def __add__(self, other):
        """Addition: species + 50"""
        if isinstance(other, int):
            return Species(
                self.scientific_name,
                self.common_name,
                self.population + other,
                self.habitat
            )
        return NotImplemented

    def __sub__(self, other):
        """Subtraction: species - 20"""
        if isinstance(other, int):
            return Species(
                self.scientific_name,
                self.common_name,
                max(0, self.population - other),
                self.habitat
            )
        return NotImplemented

    # === CONTAINER ===

    def __len__(self):
        """len(species) - number of observations"""
        return len(self.observations)

    def __getitem__(self, index):
        """species[0] - access to observations"""
        return self.observations[index]

    def __contains__(self, location):
        """'Serengeti' in species - check location"""
        return any(obs["location"] == location for obs in self.observations)

    # === CALLABLE ===

    def __call__(self, location, count):
        """Call like a function - add observation"""
        observation = {
            "date": datetime.now().strftime("%Y-%m-%d"),
            "location": location,
            "count": count
        }
        self.observations.append(observation)
        return f"Added: {count}x {self.common_name} in {location}"

    # === BOOL ===

    def __bool__(self):
        """bool(species) - does the species have any population?"""
        return self.population > 0

# === DEMONSTRATION OF ALL MAGIC METHODS ===

print("=== CREATING SPECIES ===\n")

lion = Species("Panthera leo", "Lion", 120, "savanna")
elephant = Species("Loxodonta africana", "Elephant", 450, "savanna")
rhino = Species("Diceros bicornis", "Rhino", 45, "savanna")
extinct = Species("Dodo", "Dodo", 0, "Mauritius")

# __str__ and __repr__
print("__str__ (print):")
print(lion)  # "Lion (120 individuals)"

print("\n__repr__ (repr):")
print(repr(lion))
# Species(scientific_name='Panthera leo', common_name='Lion', population=120, habitat='savanna')

# Comparisons (__eq__, __lt__, etc.)
print("\n=== COMPARISONS ===")
print(f"lion == elephant? {lion == elephant}")  # False
print(f"rhino < lion? {rhino < lion}")  # True (45 < 120)
print(f"elephant > lion? {elephant > lion}")  # True (450 > 120)

# Sorting (works through __lt__)
species_list = [lion, rhino, elephant]
species_list.sort()
print(f"\nSorted (ascending): {[str(s) for s in species_list]}")
# ['Rhino (45 individuals)', 'Lion (120 individuals)', 'Elephant (450 individuals)']

# Arithmetic (__add__, __sub__)
print("\n=== ARITHMETIC ===")
more_lions = lion + 30  # Increase population
print(f"lion + 30 = {more_lions}")  # "Lion (150 individuals)"

fewer_rhinos = rhino - 10
print(f"rhino - 10 = {fewer_rhinos}")  # "Rhino (35 individuals)"

# Callable (__call__)
print("\n=== CALLABLE - adding observations ===")
print(lion("Serengeti", 12))  # Call like a function!
print(lion("Masai Mara", 8))
print(elephant("Amboseli", 35))

# Container (__len__, __getitem__, __contains__)
print("\n=== CONTAINER ===")
print(f"Number of lion observations: {len(lion)}")  # __len__
print(f"First observation: {lion[0]}")  # __getitem__
print(f"'Serengeti' in lion? {'Serengeti' in lion}")  # __contains__ - True

# Iteration (through __getitem__)
print("\nAll lion observations:")
for obs in lion:
    print(f"  - {obs['date']} in {obs['location']}: {obs['count']} individuals")

# Bool (__bool__)
print("\n=== BOOL ===")
print(f"bool(lion)? {bool(lion)}")  # True - has population
print(f"bool(extinct)? {bool(extinct)}")  # False - population 0

if lion:
    print("Lion has a population!")

if not extinct:
    print("Dodo is extinct...")

# Hash (__hash__) - works in set and dict
print("\n=== HASH - usage in set/dict ===")
species_set = {lion, elephant, rhino}
print(f"Species set: {len(species_set)} elements")

species_dict = {
    lion: "Predator",
    elephant: "Herbivore",
    rhino: "Herbivore"
}
print(f"Lion type: {species_dict[lion]}")

List of key magic methods

Initialization and representation

  • __init__(self, ...) - constructor
  • __new__(cls, ...) - creates the object; Python calls it before __init__ (rarely overridden)
  • __str__(self) - string for the user (print)
  • __repr__(self) - string for the programmer (repr)
  • __format__(self, format_spec) - formatting (f"{obj:spec}")

Comparisons

  • __eq__(self, other) - equality ==
  • __ne__(self, other) - inequality !=
  • __lt__(self, other) - less than <
  • __le__(self, other) - less than or equal <=
  • __gt__(self, other) - greater than >
  • __ge__(self, other) - greater than or equal >=

Arithmetic

  • __add__(self, other) - addition +
  • __sub__(self, other) - subtraction -
  • __mul__(self, other) - multiplication *
  • __truediv__(self, other) - division /
  • __floordiv__(self, other) - floor division //
  • __mod__(self, other) - modulo %
  • __pow__(self, other) - exponentiation **

Container

  • __len__(self) - length len()
  • __getitem__(self, key) - read obj[key]
  • __setitem__(self, key, value) - write obj[key] = value
  • __delitem__(self, key) - delete del obj[key]
  • __contains__(self, item) - membership item in obj
  • __iter__(self) - iterator for x in obj
  • __next__(self) - next element

Callable and Context Manager

  • __call__(self, ...) - call obj()
  • __enter__(self) - enter with
  • __exit__(self, ...) - exit with

Other

  • __bool__(self) - conversion to bool
  • __hash__(self) - hash for set/dict
  • __del__(self) - destructor (rarely used)

Summary

In this lesson you learned:

  • What magic methods (dunder methods) are
  • How to make classes behave like built-in types
  • Implementing __str__, __repr__, __eq__, __lt__
  • Arithmetic operators (__add__, __sub__, __mul__)
  • Container behaviors (__len__, __getitem__, __contains__)
  • Callable objects with __call__
  • Context managers with __enter__ and __exit__
  • Practical example with a complete cataloging system

Checkpoint

Before moving on:

  • You understand the difference between __str__ and __repr__
  • You can implement comparisons
  • You know how to add arithmetic operators
  • You understand __len__, __getitem__, __iter__
  • You can create a callable object with __call__

Safari Analogy: Magic methods are animal instincts - a lion doesn't learn to roar, it just does it! Your classes can have natural behaviors too!

In the next lesson Darwin will teach you type hints - how to precisely describe data types so your code is more readable and safe!

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. What is the difference between __str__ and __repr__?

  2. 2. When is the __len__ method called?

These are 2 of 3 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Vertical ordering

    Arrange the object lifecycle:

  • Code editor

    Add a __str__ method to the Animal class that returns 'Animal: {name}'

  • Code editor

    In the Animal(name, species) class, add an __eq__ method that compares animals by species (same species = equal)

  • Click in order

    Click elements in the correct order:

  • Click in order

    Click the parts of the header of the method that print(object) calls:

  1. Put in orderObject Lifecycle
  2. Question__str__ vs __repr__
  3. Question__len__
  4. Question__eq__
  5. Coding task__str__ Method
  6. Coding task__eq__ Method
  7. Click in orderBuilding isinstance()
  8. Click in orderMagic Method

Useful articles