The @snoop decorator provides a play-by-play log of a function, showing which lines ran and when local variables changed. You can use it as a decorator or as a context manager for specific blocks of code.
To use the decorator, import snoop and apply @snoop to your function. If you prefer not to use the magical import, you can use snoop.snoop or from snoop import snoop.
To trace only a specific part of a function, wrap that section in a with snoop: block.
import snoop
@snoop
def number_to_bits(number):
if number:
bits = []
while number:
number, remainder = divmod(number, 2)
bits.insert(0, remainder)
return bits
else:
return [0]
number_to_bits(6)