Skip to main content
grasp.study
About
Explore
Pricing
Log in
Try it for free
Back to course
👋
Welcome to a Grasp lesson
Create a free account
to keep studying and create your own study path.
Create Pythonic Course Content
·
Module 6
Design Elegant Functions: Closures, Decorators, and APIs
1
Function Signatures: Positional-Only, Keyword-Only, *args, and **kwargs
Design clear function signatures using positional-only, keyword-only, `*args`, and `**kwargs`.
2
Mutable Default Arguments: A Pitfall to Avoid
Identify and correct the mutable default argument anti-pattern.
3
Higher-Order Functions
Pass functions as arguments and return them from other functions to implement callbacks and factories.
4
Closures and the `nonlocal` Keyword
Write closures that capture state from their enclosing scope and use the `nonlocal` keyword to modify that state.
Decorators: Preserving Metadata with `functools.wraps`
Write a custom decorator and apply `functools.wraps` to preserve the original function's metadata.
6
Simplifying APIs with `functools`
Use `functools.partial` for API simplification, `lru_cache` for memoization, and `singledispatch` for ad-hoc polymorphism.
7
Choosing Iteration Tools: Map, Filter, Comprehensions, Generators, and Lambdas
Choose between `map`/`filter` and comprehensions/generators, using lambdas only for short, throwaway functions.
8
Crafting Pure Functions for Robust Applications
Design small, testable, pure functions with side-effects pushed to the edges of the application.
Previous module
Choose the Right Tool: Mastering Python's Containers
Next module
Flatten Your Code: Control Flow Without the Nesting Hell