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 - Python Tutorial
From the course: Python: Design Patterns
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
-
-
-
-
-
(Locked)
Decorator1m 5s
-
(Locked)
Decorator example4m 43s
-
(Locked)
Solution: Decorator1m 55s
-
Proxy1m 22s
-
(Locked)
Proxy example5m 23s
-
(Locked)
Adapter48s
-
(Locked)
Adapter example6m 16s
-
(Locked)
Composite1m 21s
-
(Locked)
Composite example5m 25s
-
Solution: Composite1m 44s
-
(Locked)
Bridge1m 14s
-
(Locked)
Bridge example5m 31s
-
(Locked)
-
-
-