Prerequisites & Notation

Before You Begin

This chapter builds on core Python fluency from Chapters 1-3. You should be comfortable writing classes, decorators, and closures before adding type annotations, tests, and packaging around them.

  • Functions, closures, and decorators (Chapter 2)(Review ch02)

    Self-check: Can you write a decorator that logs function call arguments and return values?

  • Classes, dataclasses, and Protocols (Chapter 3)(Review ch03)

    Self-check: Can you define a Protocol with two abstract methods and verify a class satisfies it?

  • NumPy array basics: shapes, dtypes, broadcasting

    Self-check: Can you create a (3, 4) array and broadcast-add a (4,) vector to it?

  • Command-line Python: running scripts, installing packages(Review ch01)

    Self-check: Can you run pip install pytest in a virtual environment and execute pytest from the terminal?

Notation for This Chapter

Symbols and conventions introduced in this chapter for type annotations, testing, and project structure.

SymbolMeaningIntroduced
β€˜Tβ€˜`T`Generic type variable, bound via TypeVar('T')s01
β€˜X∣Noneβ€˜`X | None`Union type (Python 3.10+ syntax), equivalent to Optional[X]s01
β€˜assertβ€˜`assert`Runtime assertion statement β€” disabled when Python runs with -O flags02
β€˜@pytest.fixtureβ€˜`@pytest.fixture`Decorator marking a function as a test fixture (setup/teardown)s03
β€˜@pytest.mark.parametrizeβ€˜`@pytest.mark.parametrize`Decorator that generates multiple test cases from parameter listss03
β€˜breakpoint()β€˜`breakpoint()`Built-in function that drops into the debugger (PEP 553)s04
β€˜pyproject.tomlβ€˜`pyproject.toml`Unified Python project configuration file (PEP 518, PEP 621)s05