Exercise: Decorators
Questions for: Decorators
What is the primary use case for the
@contextlib.redirect_stdout decorator?
A:
Converts a method to a class method
B:
Redirects standard output to a specified file or file-like object
C:
Marks a method as a static method
D:
Decorates a method with dynamic behavior
Answer: B
The
@contextlib.redirect_stdout decorator is used to redirect standard output to a specified file or file-like object within a context.
What is the primary use case for the
@contextlib.ExitStack?
A:
Converts a method to a class method
B:
Manages a stack of cleanup callbacks for managing resources
C:
Marks a method as a static method
D:
Decorates a method with dynamic behavior
Answer: B
The
@contextlib.ExitStack is used for managing a stack of cleanup callbacks, making it useful for managing resources within a context.Discuss About this Question.
What is the purpose of the
@functools.lru_cache decorator's maxsize parameter?
A:
The maximum number of unique arguments to be cached
B:
The maximum size of the cache in bytes
C:
The maximum number of function calls to be cached
D:
The maximum number of distinct values to be cached
Answer: C
The
@functools.lru_cache decorator's maxsize parameter controls the maximum number of function calls to be cached.Discuss About this Question.
What is the primary use case for the
@contextlib.suppress decorator?
A:
Converts a method to a class method
B:
Marks a method as a static method
C:
Suppresses specified exceptions within a context
D:
Decorates a method with dynamic behavior
Answer: C
The
@contextlib.suppress decorator is used to suppress specified exceptions within a context, allowing the code to continue execution even if those exceptions are raised.Discuss About this Question.
What is the purpose of the
@contextlib.asynccontextmanager decorator?
A:
Converts a method to a class method
B:
Enables the use of the 'with' statement for resource management in asynchronous code
C:
Marks a method as a static method
D:
Decorates a method with dynamic behavior
Answer: B
The
@contextlib.asynccontextmanager decorator is used for creating asynchronous context managers, enabling the use of the 'async with' statement in asynchronous code.Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.