Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Decorators | Python Fundamentals lesson | BraveProgrammer
  1. Courses
  2. /
  3. Python Fundamentals

Lesson 30 of 55

·Python

Decorators

20 minutes

Course progress55%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • Decorators take a function and return a new wrapper function.
  • Use `@decorator_name` above a function definition.
  • Common uses: logging, access control, caching, timing.

Decorators

A decorator is a higher‑order function that wraps another function to extend its behavior without modifying the original code.

def debug(func):
    def wrapper(*args, **kwargs):
        print(f'Calling {func.__name__} with args={args}, kwargs={kwargs}')
        result = func(*args, **kwargs)
        print(f'{func.__name__} returned {result}')
        return result
    return wrapper

@debug
def add(a, b):
    return a + b

add(3, 4)

The @debug line is syntactic sugar for add = debug(add).

Make it stickPractice what you've learned1 quiz · 5 practical tests · Optional activities from Python FundamentalsBrowse activities
Previous lessonMagic Methods and DunderContinue to lesson 31Generators and IteratorsBack to Python Fundamentals curriculum