August 2018
Intermediate to advanced
332 pages
9h 12m
English
The previous example requires three levels of nested functions. The first it is going to be a function that receives the parameters of the decorator we want to use. Inside this function, the rest of the functions are closures that use these parameters along with the logic of the decorator.
A cleaner implementation of this would be to use a class to define the decorator. In this case, we can pass the parameters in the __init__ method, and then implement the logic of the decorator on the magic method named __call__.
The code for the decorator will look like it does in the following example:
class WithRetry: def __init__(self, retries_limit=RETRIES_LIMIT, allowed_exceptions=None): self.retries_limit = retries_limit self.allowed_exceptions ...
Read now
Unlock full access