DEV Community

Muhammed Shafin P
Muhammed Shafin P

Posted on

functools Workbook Added to Standard Library Learning Path

I've just completed and uploaded the functools workbook to my standardlib-datastruct repository. This is part of my ongoing journey to master Python's standard library through practical exercises.

What's Inside

The functools workbook contains 15 progressive exercises covering the essential tools from Python's functools module:

  • Caching mechanisms: lru_cache, cache
  • Function tools: partial, partialmethod, wraps, update_wrapper
  • Class utilities: total_ordering, singledispatch, singledispatchmethod
  • Functional programming: reduce, cmp_to_key
  • Advanced combinations: Real-world scenarios mixing multiple functools features

Each exercise includes a clear problem statement and a complete solution with working code examples.

Extra: Cache Management Methods

While not covered in the exercises, lru_cache and cache decorated functions come with useful methods:

  • cache_info() - Returns a named tuple with cache statistics (hits, misses, currsize, maxsize). Since it's a named tuple, you can access values by index or by attribute name (e.g., info.hits or info[0])
  • cache_clear() - Clears all cached entries

These can be helpful for monitoring cache performance and managing memory usage in production code.

About This Learning Path

This workbook is not beginner-friendly by design. It assumes you understand:

  • Python decorators and how they work
  • Functions act as non-data descriptors when assigned to class-level attributes
  • Descriptors in general (I discovered partialmethod is actually a descriptor)
  • Basic functional programming concepts

I've included only the functionalities I find necessary and practical. This isn't an exhaustive reference—it's a focused learning tool for understanding the core capabilities of functools.

Python 3.11+ Features

I'm using Python 3.11 for my learning, and I've discovered some cleaner syntax improvements. For example, with singledispatch, you can now use type hints on the first argument instead of the old register(type) syntax:

# Old way (still works)
@describe.register(int)
def _(obj):
    print("Integer")

# Python 3.11+ way (cleaner!)
@describe.register
def _(obj: int):
    print("Integer")
Enter fullscreen mode Exit fullscreen mode

The type hint automatically registers the function for that type. Much cleaner and more Pythonic!

My Approach

I'm learning the standard library by:

  1. Selecting the most useful modules and functions
  2. Creating hands-on exercises that demonstrate real use cases
  3. Building a personal reference that I can return to
  4. Sharing it for others who want to follow a similar path

This repository (standardlib-datastruct) focuses on standard library tools that are useful when handling data structures. This may not include everything—I only add what I find necessary and want to learn. It's my curated selection based on practical needs.

Some standard library modules may be skipped because 3rd party or external libraries handle those use cases better. I focus on what's worth learning from the standard library itself.

This is my way of learning—practical, focused, and exercise-driven. If you're past the beginner stage and want to level up your Python skills, this might resonate with you.

Get the Workbook

The functools workbook is available now in the repository as functools.zip. It includes:

  • functools_exercises_workbook.md - Markdown format
  • functools_exercises_workbook.pdf - PDF format (generated with pandoc + MiKTeX)
  • functool_workbook.py - Source generator script

Feel free to download, work through the exercises, and adapt them to your learning style.


Repository: github.com/hejhdiss/standardlib-datastruct

Note: This is a living project. I'll continue adding more standard library modules as I complete them. Follow along if this learning approach works for you!

Top comments (0)