Python Programming - Decorators
Exercise : Decorators - General Questions
- Decorators - General Questions
26.
What is the primary purpose of the
@contextlib.contextmanager
decorator?
Answer: Option
Explanation:
The
@contextlib.contextmanager
decorator is used to define a generator-based context manager, enabling the use of the 'with' statement for resource management.
27.
What does the
@functools.cmp_to_key
decorator do?
Answer: Option
Explanation:
The
@functools.cmp_to_key
decorator is used to convert an old-style comparison function to a key function, making it compatible with functions that require a key for comparison.
28.
What is the primary purpose of the
@functools.cached_property
decorator?
Answer: Option
Explanation:
The
@functools.cached_property
decorator is used to cache the result of a method and return the cached value on subsequent calls, improving performance by avoiding redundant computations.
29.
How does the
@functools.partialmethod
decorator differ from @functools.partial
?
Answer: Option
Explanation:
@functools.partialmethod
is specifically designed for methods in a class, allowing for partial application of arguments, while @functools.partial
is used for functions in general.
30.
How does the
@contextlib.asynccontextmanager
decorator differ from @contextlib.contextmanager
?
Answer: Option
Explanation:
@contextlib.asynccontextmanager
is specifically designed for creating asynchronous context managers, allowing the use of the 'async with' statement.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers