To accept arguments to our class-based decorator, we would have to make subtle changes because of the following issues.
- The
__init__
method will now receive the decorator argument and not the function.
- The function will be passed as the first argument to
__call__
.
- The function arguments will be received in a nested function.
In the snippet above, we are using the AbbvExpander
decorator on display. We pass it an abbreviation dictionary containing the full forms of common internet slang. These are received within the __init__
method in the decorator class. The display()
function is received as an argument in the __call__
method. We define a wrapper function within the __call__
method that will receive the sentence and process it accordingly.