Returning Values From Decorated Functions
Return values from decorated functions don’t get returned by default unless the decorator allows it. In this lesson, you’ll see how to get return values out of decorated functions by making a small change to the decorator.
00:00 You passed arguments through your decorator—what about returning values from decorated functions? Let me show you an example. What happens to the return value of a decorated function? Well, it’s up to the decorator to decide what it’s going to do. Let’s take a look.
Earlier, you set it up so the
@do_twice decorator can accept arguments. In this case, this function will print
"Creating greeting" and then return an f-string with the argument that was passed in.
01:37 It looks like the decorator ate our argument and didn’t return it. How do you resolve that? Up here, inside the wrapper, change the second function to return the function being called. Let me repeat: you need to make sure that your decorator returns what’s being called, and it needs to be returned within the decorated function in order to return that value.
02:50 Yep. It looks like it’s been assigned. And if you print it, that looks great. So, what did you change? By returning the value from our last function call, the decorated function gets back the value it was expecting.
Become a Member to join the conversation.