Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

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.

Comments & Discussion

Mallesham Yamulla on Sept. 1, 2019

Returning values from Decorating functions… 1. a Decorator

def spam_result_d(func): def wrapper_spam_res(args,kwargs): #func(args,kwargs) return func(args,**kwargs) return wrapper_spam_res

2. a function

@spam_result_d def spam_result_f(res): print(‘checking for result’) print(f’result is:{res}’)

res_1 = spam_result_f(‘blocked’) print(res_1)

3. Output:

checking for result result is:blocked–2 None

Here Could you please let me know why i’m getting None on printing it’s value res_1..?

I have followed your video on this concept , and there it’s giving an expected output..

Geir Arne Hjelle RP Team on Sept. 1, 2019

Hi Mallesham

The None comes from your spam_result_f() function. It doesn’t contain a return statement. For such functions, Python implicitly returns None.

If you add something like return f"result is {res}" after your prints, you should see the return value as well.

Mallesham Yamulla on Sept. 1, 2019

Ha got it. thanks for your response.. it’s really an awesome/ a very helpfull resource for learning decorators. thanks again.

Priya katta on March 12, 2020

Hi Chris,

could you please explain me this..

def decorator(func) def wrapper(args,kwargs): func(args, kwargs) return func(*args, kwargs) print(“This is printed after previous return”) return wrapper result = func(“some string”)

How does the statement after return gets executed?..

Geir Arne Hjelle RP Team on March 12, 2020

Hi Priya,

that statement will not get executed. If you need to do things in your decorator after calling the decorated function, you should store the return value in a variable:

def decorator(func)
   def wrapper(args,kwargs):
       func(args, kwargs)
       value = func(*args, kwargs)
       print(This is printed after previous return)
       return value
   return wrapper
result = func(some string)   

Priya katta on April 14, 2020

Thank you!

Become a Member to join the conversation.