Menu
Coddy logo textTech

Practice #5

Lesson 12 of 12 in Coddy's Python Decorators course.

challenge icon

Challenge

Easy

Write a decorator named memory_usage that measures the memory usage of a function.

The decorator should print a message indicating the amount of memory used by the function before and after it is executed, in the following format:

Memory usage: 0 Bytes

To get the current "memory usage" (not exactly),

import tracemalloc
tracemalloc.start()
res = # some operations
current, peak = tracemalloc.get_traced_memory()

current will hold the value in Bytes.

Return the result of the function in the end!

Try it yourself

All lessons in Python Decorators