Python Programming - Decorators
Exercise : Decorators - General Questions
- Decorators - General Questions
16.
Which decorator is commonly used for retrying a function with a specified delay in case of failure?
Answer: Option
Explanation:
There is no built-in
@functools.retry
decorator in Python, but custom decorators can be created for retrying a function with a specified delay in case of failure.
17.
How does the
@property
decorator differ from the @staticmethod
decorator?
Answer: Option
Explanation:
@property
is used to define read-only properties without the need for explicit getter methods. @staticmethod
is used to mark a method as a static method within a class.
18.
Which decorator is commonly used for rate-limiting function calls?
Answer: Option
Explanation:
There is no built-in
@functools.ratelimit
decorator in Python, but custom decorators can be created for rate-limiting function calls.
19.
What is the purpose of the
@functools.lru_cache
decorator's typed
parameter?
Answer: Option
Explanation:
The
@functools.lru_cache
decorator's typed
parameter, when set to True
, uses a different cache for each distinct data type, preventing collisions between values of different types.
20.
What is the primary purpose of the
@functools.total_ordering
decorator?
Answer: Option
Explanation:
The
@functools.total_ordering
decorator is used to automatically generate rich comparison methods (e.g., __eq__
, __lt__
) based on a minimal set of comparison methods provided in the class.
@functools.total_ordering
class MyClass:
def __init__(self, value):
self.value = value
def __eq__(self, other):
return self.value == other.value
def __lt__(self, other):
return self.value < other.value
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers