Skip to content

This is a hands-on learning material generated by LLM to help understand how co- & contra-variance works in Python3

Notifications You must be signed in to change notification settings

KentaroJay/Learn-Co-Contra-Variance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Learn Covariance and Contravariance in Python 🦁

Welcome to the Zoo Management System! Through managing animals in our zoo, you'll learn the essential concepts of covariance and contravariance in Python's type system.

🎯 Learning Goals

By completing this tutorial, you'll understand:

  • Why variance matters in type systems
  • When to use covariance vs contravariance
  • How to implement type-safe generic classes in Python

🚀 Quick Start

# Install dependencies
pip install mypy

# Run the first practice
python -m practices.lesson1_viewing_animals

# Check your solution with mypy
mypy practices/lesson1_viewing_animals.py

📚 The Story

You're a software engineer at a zoo. Your job is to build a type-safe system for managing animals. Through four progressive lessons, you'll discover why Python's variance rules exist and how to use them effectively.

Lesson 1: Viewing Animals (Covariance) 👀

Scenario: Visitors want to view animals in exhibits. A cat exhibit should work anywhere an animal exhibit is expected.

Key Insight: Reading is safe with subtypes → Use covariance

Lesson 2: Feeding Animals (Contravariance) 🍖

Scenario: Zookeepers feed animals. A handler who can feed any animal should be able to feed cats specifically.

Key Insight: Writing is safe with supertypes → Use contravariance

Lesson 3: Animal Cages (Invariance) 🏠

Scenario: Moving animals between cages. You can't use a cat cage where an animal cage is expected (might put a dog in!).

Key Insight: Read+Write requires exact types → Use invariance

Lesson 4: Real Zoo System (Combined) 🏗️

Scenario: Build a complete zoo management system using all three concepts together.

🎮 How to Learn

Each lesson contains:

  1. Broken code with type errors
  2. Your task: Fix the code to pass mypy checks
  3. Solution: Check the answer when you're done

Start with Lesson 1 and work your way through. Each lesson builds on the previous one!

📁 Project Structure

practices/
├── lesson1_viewing_animals.py    # Covariance practice
├── lesson2_feeding_animals.py    # Contravariance practice
├── lesson3_animal_cages.py       # Invariance practice
├── lesson4_zoo_system.py         # Combined practice
└── solutions/                    # Check your answers here
zoo/
└── animals.py                    # Base classes used throughout

🔍 Quick Reference

Concept Use When Example
Covariance Read-only operations Exhibit[Cat]Exhibit[Animal]
Contravariance Write-only operations Feeder[Animal]Feeder[Cat]
Invariance Read and write operations Cage[Cat]Cage[Animal]

Ready to start? Head to practices/lesson1_viewing_animals.py! 🚀

About

This is a hands-on learning material generated by LLM to help understand how co- & contra-variance works in Python3

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages