By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Decorators in Python are a powerful tool for modifying the behavior of functions or methods. @timer, @debug, and @login_required are common decorators that enhance functionality, such as timing execution, debugging, and enforcing login requirements. Mastering these decorators is crucial for writing efficient, maintainable, and secure code. Incorrect usage can lead to performance issues, security vulnerabilities, or difficult-to-trace bugs. For instance, failing to use @login_required properly can expose sensitive data to unauthorized users.
python def my_decorator(func): def wrapper(): print("Something is happening before the function is called.") func() print("Something is happening after the function is called.") return wrapper
⚠️ Pitfall: Forgetting to return the wrapper function.
Apply a Decorator
@
python @my_decorator def say_hello(): print("Hello!")
⚠️ Pitfall: Misplacing the @ symbol.
Create the @timer Decorator
time
Example: ```python import time
def timer(func): def wrapper(args, kwargs): start_time = time.time() result = func(args, kwargs) end_time = time.time() print(f"Execution time: {end_time - start_time} seconds") return result return wrapper ``` - ⚠️ Pitfall: Not handling arguments correctly.
Create the @debug Decorator
python 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
⚠️ Pitfall: Forgetting to print the return value.
Create the @login_required Decorator
Example: ```python logged_in = False
def login_required(func): def wrapper(args, kwargs): if not logged_in: raise Exception("User not logged in") return func(args, kwargs) return wrapper ``` - ⚠️ Pitfall: Not raising an exception for unauthorized access.
Experts view decorators as a way to separate concerns. Instead of cluttering functions with timing, debugging, or authentication logic, they use decorators to keep code clean and focused. This modular approach makes the codebase easier to maintain and extend.
Exam trap: Questions that require identifying why a decorator doesn't work.
The mistake: Not handling function arguments correctly.
*args
kwargs
Exam trap: Functions with multiple arguments.
The mistake: Misplacing the @ symbol.
Exam trap: Code snippets with incorrect decorator placement.
The mistake: Forgetting to print the return value in the @debug decorator.
@debug
Exam trap: Questions that require tracing function execution.
The mistake: Not raising an exception for unauthorized access in the @login_required decorator.
@login_required
Scenario: You need to measure the execution time of a function that sorts a list. Question: Write a function that uses the @timer decorator to sort a list. Solution: python @timer def sort_list(lst): return sorted(lst) Answer: The function will print the execution time. Why it works: The @timer decorator measures the time taken to sort the list.
@timer
python @timer def sort_list(lst): return sorted(lst)
Scenario: You need to debug a function that calculates the factorial of a number. Question: Write a function that uses the @debug decorator to calculate the factorial. Solution: python @debug def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1) Answer: The function will print debug information. Why it works: The @debug decorator prints the function name, arguments, and return value.
python @debug def factorial(n): if n == 0: return 1 else: return n * factorial(n - 1)
Scenario: You need to secure a function that accesses user data. Question: Write a function that uses the @login_required decorator to access user data. Solution: python @login_required def access_user_data(): return "User data accessed" Answer: The function will raise an exception if the user is not logged in. Why it works: The @login_required decorator checks the login status before executing the function.
python @login_required def access_user_data(): return "User data accessed"
def decorator(func): def wrapper(*args, kwargs): ... return wrapper
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.