Udemy
    •  
    •  
    •  
    •  
    •  
    •  
    •  
    •  
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Intermediate Python: Master Decorators From Scratch
Rating: 4.7 out of 5(265 ratings)
17,772 students

Intermediate Python: Master Decorators From Scratch

A Comprehensive Introduction to Decorators In Python with Several Practical Applications
Created byAndy Bek
Last updated 7/2026
English
English [Auto],

What you'll learn

  • Understand what decorators are and how they are used in python functional programming
  • Write advanced, flexible decorators to handle caching, performance timing, logging, and more
  • Master the syntax and, more importantly, the core ideas behind decorators
  • Get ample practice through 12 coding assignments, followed by detailed solutions

Course content

1 section36 lectures2h 52m total length
  • VERY Short Intro!1:09

    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.

  • Course Resources0:07
  • Introduction to Python Functions7:03

    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.

  • Skill Challenge: Averaging Grades0:53

    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.

  • Solution2:56

    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.

  • More On Functions: *args and **kwargs14:06

    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.

  • Skill Challenge: Variadics1:20

    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.

  • Solution2:40

    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.

  • Higher-Order Functions7:59

    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.

  • Skill Challenge: Arithmetic HOF1:26

    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.

  • Solution2:25

    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.

  • First-Class Functions6:12

    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.

  • Closures7:51

    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.

  • Skill Challenge: Counter Factory2:10

    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.

  • Solution6:28

    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.

  • Basic Introduction To Decorators8:21

    Discover Python decorators that modify functions without changing their source, using the at sign and pie decorator syntax, with callables and first-class objects.

  • Decorating Parameterized Functions5:47

    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.

  • Skill Challenge - Let's Log1:46

    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.

  • Solution3:07

    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.

  • Skill Challenge - Lotto Draws1:04

    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.

  • Solution3:07

    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.

  • Skill Challenge - Writing A Timer0:43

    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.

  • Solution5:26

    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.

  • Decorators With Arguments8:04

    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.

  • Skill Challenge - Repeated Lotto Draws1:05

    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.

  • Solution2:22

    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.

  • Chaining Multiple Decorators5:32

    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.

  • Preserving Identity With @wraps9:07

    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.

  • Skill Challenge - Delaying Downloads3:00

    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.

  • Solution6:40

    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.

  • Skill Challenge - Authentication Workflow Part I2:09

    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.

  • Solution Part I12:45

    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.

  • Skill Challenge - Authentication Workflow Part II2:06

    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.

  • Solution Part II11:02

    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.

  • Skill Challenge - Building A Cache3:00

    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.

  • Solution11:30

    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.

Requirements

  • Some familiarity with python is preferred, though not strictly required

Description

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!

Who this course is for:

  • Beginner Python developers interested in stepping up into intermediate and advance topics