Join Jungwoo Ryoo for an in-depth discussion in this video Decorator example, part of Design Patterns with Python.
- Let's define our decorator by first making…our decorator being transparent…especially in terms of its name and doc string.…So we use this special function…@wraps…and then we pass the function we are decorating…so by doing this whatever we are using to decorate…the function the effect of it will be transparent.…
Now let's define what the decorator is doing…by working on the inner function definition.…First of all we'll try to grab the return value…of the function that's actually being decorated.…So we'll say ret,…our variable assignment…and whatever is being returned from the function…we're getting in the decorator.…And next we'll be doing our magic to further process…whatever is being returned from the function…being decorated.…
So in this case we're simply adding a new tag…which is blink tag in HTML.…So type return.…So we're not just returning whatever we got from…the original function…but with this additional string.…Blink tag and then we'll append…the blink tag to the returned value…of the original function which is…
- Understanding design patterns
- Best design practices: consistency, completeness, and correctness
- Working with creational patterns
- Working with structural patterns
- Working with behavioral patterns
Skill Level Intermediate
Q: In the strategy pattern example, why does the code keep executing the default function rather than the alternate?
A: The programming demonstration skips the step of defining what the strategy pattern should be when an alternate function name is provided
as an argument as shown below.
Foundations of Programming: Design Patternswith Elisabeth Robson2h 19m Intermediate
1. Understanding Design Patterns
2. Creational Patterns
3. Structural Patterns
4. Behavioral Patterns
5. Design Best Practices
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.