Locked learning resources

You must own this product to watch this lesson.

Locked learning resources

You must own this product to watch this lesson.

Returning Values From Decorated Functions

This lesson is from the Real Python video course by Christopher Bailey.

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.

00:19 I started a new REPL session. From decorators I’ll have you import again. Okay. Define the new function, decorating with @do_twice.

00:32 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.

00:50 Looks good! In this case, I’ll have you return it into a new variable, name it hi_adam, and assign it by putting in the return_greeting() function, "Adam".

01:06 This argument will pass in to the name, and this should be returned into here. Great. What happens? It simply said Creating greeting, Creating greeting—doing it twice. Okay.

01:21 Let’s look at what’s going on with hi_adam. Using a print statement on hi_adam shows None, and if you type the object’s name, it shows nothing also.

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:09 So, save. Then down here, I need to exit out and restart my REPL.

02:21 Okay. from decorators import do_twice. Decorate your function the same way as last time, and this time when calling it…

02:42 Great. It looks like it returned it. So can you assign it to something?

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.

03:10 You’re doing fantastic so far. One last little concept to cover and then on to some real-world examples. So up next, you’ll do a little bit of function introspection.

You must own this product to join the conversation.