Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Writing Functions in Python

Cursus bekijken

Oefeninstructies

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

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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}))
Code bewerken en uitvoeren