课程: Python: Design Patterns (2021)
今天就学习课程吧!
今天就开通帐号,24,700 门业界名师课程任您挑!
Decorator example
- 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…