Question 1
What is the output of the following code?
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
Question 2
In python ,can you use more than one decorator on a single function?
Question 3
What will be the output of the following code?
def logger(func):
def wrapper(*args, **kwargs):
print(f"Calling {func.__name__} with args: {args} and kwargs: {kwargs}") result = func(*args, **kwargs)
print(f"{func.__name__} returned: {result}")
return result
return wrapper
@logger
def add(a, b):
return a + b
result = add(3, 5)
Question 4
What is the output of the following code?
def my_decorator(func):
def wrapper(*args, **kwargs):
print("Before function execution")
result = func(*args, **kwargs)
print("After function execution")
return result
return wrapper
@my_decorator
def add(a, b):
return a + b
result = add(3, 5)
Question 5
In the previous code , what does @repead(3) do?
Question 6
What is the primary purpose of the @logger decorator in the previous code?
Question 7
What is the purpose of the @my_decorator line in the previous code?
Question 8
What does the following code do?
def repeat(n):
def decorator(func):
def wrapper(*args, **kwargs):
for _ in range(n):
func(*args, **kwargs)
return wrapper
return decorator
@repeat(3)
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
Question 9
What is the primary purpose of the wrapper function in decorator code?
Question 10
What does the following code do?
def uppercase_decorator(func):
def wrapper(text):
result = func(text)
return result.upper()
return wrapper
@uppercase_decorator
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))