Module 20 · Building Real Python Applications — topic 4 of 13
Functions and modularity
Building applications from small reusable functions: single-purpose functions, parameters instead of globals, pure functions versus functions with side effects, reusable helpers shared by several features, and grouping functions into modules.
Learning objectives
- Write small, single-purpose functions
- Distinguish pure functions from those with side effects
- Reuse helpers across features
Definition
Modularity means building a program from independent, reusable pieces. In practice, that starts with small functions that do one thing, receive what they need as parameters and return results, grouped into modules by topic.
Introduction
The expense tracker needs monthly totals and category totals. With one reusable group_by helper, both features are two lines each.
Why it matters
Small, reusable functions reduce duplication, make testing easy and let features be built by combining existing pieces.
Detailed explanation
A function should do one thing that its name describes. If it parses, calculates and prints, split it.
Pass data in as parameters and return results; avoid reading or modifying global variables.
Pure functions — no side effects, same output for the same input — are the easiest to test and reuse.
Some functions must have side effects (saving files, printing). Keep them at the edges of the program and keep them small.
Decide whether a function modifies its argument or returns a new value, and make the name and docstring say so.
Look for general helpers hiding in specific code: grouping, formatting money, parsing dates. Reuse them across features.
Group related functions into modules (money.py, dates.py, reports.py) once the main file grows.
Default parameters and keyword arguments make helpers flexible without duplicating them.
Syntax
- One job per function.
- Parameters in, results out.
- Reuse general helpers.
Code examples
Small, single-purpose functions
₹1,234.57
Pure function versus side effect
[100] [100, 50] [100, 75]
More examples
One helper, two features
{'2026-10': 290, '2026-11': 120}
{'food': 370, 'travel': 40}Step-by-step explanation
- group_by knows nothing about expenses; it groups any items by any key function.
- The monthly report and the category report differ only in the key they pass.
- A new 'by week' report would need one more line, not a new loop.
Expected output
Running “One helper, two features” prints exactly:
Practical applications
- Shared utility modules in large code bases.
- Reporting features built from common grouping and formatting helpers.
Best practices
- Keep functions short and focused.
- Prefer pure functions for logic.
- Extract general helpers and reuse them.
Common mistakes
Functions that rely on globals
Avoid
Prefer
A parameter makes the dependency visible and the function reusable.
Important notes
- functools.partial can pre-fill arguments to create specialised versions of general helpers.
Key takeaways
- Build applications from small single-purpose functions.
- Pass data in and return results.
- Reuse general helpers across features.
Try it yourself
- 1Write a by-week report using group_by and isocalendar().
- 2Move the money and date helpers of a project into their own modules.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- AdvancedWhat is printed?