🎓 All Courses | 📚 Python Programming Syllabus

📋 Study this course on TaskLoco
#python-programming#decorators#advanced

Decorators — Wrapping Functions

A decorator wraps a function to add behavior without modifying its code.


Basic decorator:

def my_decorator(func):
    def wrapper(*args, **kwargs):
        print("Before function")
        result = func(*args, **kwargs)
        print("After function")
        return result
    return wrapper

@my_decorator
def say_hello():
    print("Hello!")

say_hello()
# Before function
# Hello!
# After function

Common built-in decorators:

class MyClass:
    @staticmethod
    def static_method():     # no self needed
        pass

    @classmethod
    def class_method(cls):   # gets class, not instance
        pass

    @property
    def name(self):          # access like attribute
        return self._name

Real-world use: Timing functions, logging, authentication checks, caching results (@functools.lru_cache).


YouTube • Top 10
Python Programming: Decorators
Tap to Watch ›
📸
Google Images • Top 10
Python Programming: Decorators
Tap to View ›

Reference:

Wikipedia: Python Syntax

image for linkhttps://en.wikipedia.org/wiki/Python_syntax_and_semantics

📚 Python Programming — Full Course Syllabus
📋 Study this course on TaskLoco

TaskLoco™ — The Sticky Note GOAT