
Master Python basics in a quick, hands-on intro. Learn to run Python in a Jupyter-based environment across cloud and local setups, with pointers to web scraping, databases, pandas, and more.
Introduce Python functions, covering definition and invocation, parameters and default values, and the distinction between print and return, plus pure versus impure functions as groundwork for decorators.
Define the calculate_average function to process a list of numbers, print a message when the list is empty, then compute and return the mean while reporting count and average.
Define a function to calculate the average of numbers, perform an empty input check, use sum and len, print the results, and test with a sample list.
Master Python function signatures by using star arcs and double star quarks to accept any number of positional and keyword arguments, built with tuples and dictionaries.
Practice variadics by refactoring the calculate average function to accept a variable number of positional arguments. Add an optional rounding keyword argument to specify decimal places and provide reasonable defaults.
In this lecture, the instructor refactors a function to accept arbitrary positional arguments via a single asterisk, track counts, add an optional keyword argument, and return the rounded average.
Explore higher-order functions in Python by passing functions as arguments and returning wrapped functions, enabling reusable, modular behavior and paving the way toward decorators.
Learn to define a higher order function in Python that doubles the output of another function, using a basic add function to illustrate the concept and its decorator-like behavior.
Implement a higher-order function in Python that wraps an add function to produce a doubled version, using an inner function with arbitrary arguments and validating results like 6 and 32.
Explore how Python treats functions as first-class objects and master higher-order functions. Practice passing, returning, and annotating callables with lambdas, map, filter, and reduce to unlock flexible coding patterns.
Explore closures in Python, where a function retains access to its outer scope values, enabling private state, data hiding, and function factories like make multiplier.
Explore a create counter factory that returns a counter function with its own state. Each instance tracks its count independently, starting from a configurable start value, incrementing on each call.
Explore building a counter factory with a closure, using a mutable list to maintain state, and learn how start values and higher-order functions yield reusable counters.
Discover Python decorators that modify functions without changing their source, using the at sign and pie decorator syntax, with callables and first-class objects.
Learn how to create flexible decorators in Python by using *args and **kwargs to decorate functions with any parameter signature, improving reusability and reducing code duplication.
Define a new logger decorator that prints function name, args, kwargs, and the return value when a function runs, demonstrating with a and b, and emphasizing positional and keyword arguments.
Define a generic logger decorator that wraps a function with *args and **kwargs, logs the function name and arguments, and returns the original result after testing with a decorated function.
Define a generic repeat decorator that invokes a function twice, regardless of its arguments. Decorate a lotto draw function to return two random integers from start to end inclusively.
Define a repeat decorator that calls a function twice using *args and **kwargs, then apply it to a lotto draw function that uses random.randint to return two numbers per invocation.
Implement a Timed decorator to measure and print a function’s execution time in seconds, then decorate a function that runs for a noticeable duration to test it.
Define a timed decorator in Python that wraps a function, measures execution time with perf_counter, prints the function name and duration, and returns the original result.
Master decorators with arguments to enforce a flexible workout calorie target, using an outer function that returns a decorator and a wrapper with *args and **kwargs.
Generalize the repeat decorator to invoke any target function n times, as specified, and return a Python list of all numeric outputs (integers, floats, or random outcomes) in sorted order.
Define a parameterized repeat decorator that calls a function num times, collects numeric outputs in a results list, and returns the sorted list of those results.
Learn decorator chaining in Python by applying multiple decorators to a function, understand bottom-to-top order, and see how output becomes input, with split and uppercase examples.
Preserve function identity when decorating by using functools wraps to copy the original function's metadata, including name and docstring, to the wrapper and expose __wrapped__ for debugging and docs.
Implement a decorator to progressively delay downloads per user-resource pair, starting with immediate links and doubling the delay on each subsequent request, using a placeholder download function.
Define a download function using uuid, then implement a delay decorator with a wrapper that tracks user IDs and applies progressive delay from 1 to 8 seconds to deter DDoS.
Explore how decorators enable an authentication workflow by building a mock, modular menu interface with four options—view roster, upvote, add to roster, and quit—storing data locally for later decorator use.
Build a console roster manager with a looping menu, upvote and add-to-roster actions, and a sorted, vote-based roster display using a list of dicts.
Create an authentication workflow with a decorator that prompts for a username and password before function calls. Apply it to upvote and add-to-roster actions to require authentication.
Define an auth decorator as a higher-order wrapper that prompts for credentials, validates against local state, preserves metadata with wraps, and enables authenticated upvote and roster management.
Build a caching mechanism for a get weather function that simulates a one-second delay and returns city-specific temperature and humidity, serving cached results if called within ten seconds.
Build a from-scratch cache decorator for a weather function, storing city-specific data with timestamps in a dictionary and refreshing after 10 seconds, demonstrating cached and fresh results.
Welcome to the best and most complete guide to Python decorators!
In this course, you will cultivate a deep and practical understanding of decorators in Python, starting from the very basics. This beginner-friendly course is meticulously crafted to ensure you build a solid foundation from scratch while also getting ample practice in the process.
Before diving into decorators, we'll explore the fundamental principles that serve as the building blocks of this intriguing topic. You'll gain a deep understanding of higher-order functions, closures, and first-class functions – prerequisites that unlock the magic of decorators. Foundational topics include:
Closures
First-Class Functions
Variadics
Higher Order Functions
Once we have established a solid grasp of these functional foundations, we'll dive into the captivating world of decorators. We'll start with a gentle introduction and gradually move towards more intricate techniques. Our exploration of decorators will include:
Basic Decorators
Decorating Parameterized Functions
Chaining Decorators
Decorating Functions With Variable Arity
Preserving Metadata of Decorated Functions
But that's not all! Each concept will be reinforced through engaging coding assignments of increasing difficulty. Throughout the course, will also explore several skill challenges inspired by real-world scenarios including:
Logging Function Output
Timing Runtime Performance
Using Decorators In Mocked Authentication Flows
Creating Cache Mechanisms
This course is tailored specifically for Python programmers interested in deepening their understanding of decorators as a gateway to functional programming or as a standalone, intermediate-level programming pattern.
See you inside!