Get startedGet started for free

Print the return type

You are debugging a package that you've been working on with your friends. Something weird is happening with the data being returned from one of your functions, but you're not even sure which function is causing the trouble. You know that sometimes bugs can sneak into your code when you are expecting a function to return one thing, and it returns something different. For instance, if you expect a function to return a numpy array, but it returns a list, you can get unexpected behavior. To ensure this is not what is causing the trouble, you decide to write a decorator, print_return_type(), that will print out the type of the variable that gets returned from every call of any function it is decorating.

This exercise is part of the course

Writing Functions in Python

View Course

Exercise instructions

  • Create a nested function, wrapper(), that will become the new decorated function.
  • Call the function being decorated.
  • Return the new decorated function.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

def print_return_type(func):
  # Define wrapper(), the decorated function
  ____ ____(____, ____):
    # Call the function being decorated
    result = ____(____, ____)
    print('{}() returned type {}'.format(
      func.__name__, type(result)
    ))
    return result
  # Return the decorated function
  return ____
  
@print_return_type
def foo(value):
  return value
  
print(foo(42))
print(foo([1, 2, 3]))
print(foo({'a': 42}))
Edit and Run Code