1. Learn
  2. /
  3. Courses
  4. /
  5. Writing Functions in Python

Connected

Exercise

Check the return type

Python's flexibility around data types is usually cited as one of the benefits of the language. It can sometimes cause problems though if incorrect data types go unnoticed. You've decided that in order to ensure your code is doing exactly what you want it to do, you will explicitly check the return types in all of your functions and make sure they're returning what you expect. To do that, you are going to create a decorator that checks if the return type of the decorated function is correct.

Note: assert is a keyword that you can use to test whether something is true. If you type assert condition and condition is True, this function doesn't do anything. If condition is False, this function raises an error. The type of error that it raises is called an AssertionError.

Instructions 1/2

undefined XP
    1
    2
  • Start by completing the returns_dict() decorator so that it raises an AssertionError if the return type of the decorated function is not a dictionary.