From the course: Python: Design Patterns

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Decorator example

Decorator example

- [Instructor] Let's define our decorator first. We use the special Python keyword @wraps and pass the function we're decorating. By doing this, whatever we are using to decorate the function and its effect will be transparent. Now let's define what the decorator is doing by working on the inner function definition. First, we'll grab the return value of the function being decorated. Type ret, our variable, space, assignment, space, and whatever is being returned from the function we're getting in the decorator. Next, we'll do our magic to further process the return value of the function being decorated. In this case, we add a new HyperText Markup Language or HTML tag blink around the original string. Type return, space, and the blink tag, we'll append the blink tag to the return value of the original function, which is ret. And then we'll close the tag. Now, whatever we did in the inner function will be returned by this statement, return decorator. Before we apply our decorator, let's…

Contents