Codementor Events

Python: trace recursive function

Published Mar 31, 2019
Python: trace recursive function

During my way on Software Engineering path I’m facing problems of different kind. And I love when it’s time to solve some “pure programming” task. Let’s take a look on the assignment I was asked to help with.

recursion.png

After an hour of work, I've ended up the solution. Let me share the result code with comments:

from functools import wraps


# decorator to trace execution of recursive function
def trace(func):

    # cache func name, which will be used for trace print
    func_name = func.__name__
    # define the separator, which will indicate current recursion level (repeated N times)
    separator = '|  '

    # current recursion depth
    trace.recursion_depth = 0

    @wraps(func)
    def traced_func(*args, **kwargs):

        # repeat separator N times (where N is recursion depth)
        # `map(str, args)` prepares the iterable with str representation of positional arguments
        # `", ".join(map(str, args))` will generate comma-separated list of positional arguments
        # `"x"*5` will print `"xxxxx"` - so we can use multiplication operator to repeat separator
        print(f'{separator * trace.recursion_depth}|-- {func_name}({", ".join(map(str, args))})')
        # we're diving in
        trace.recursion_depth += 1
        result = func(*args, **kwargs)
        # going out of that level of recursion
        trace.recursion_depth -= 1
        # result is printed on the next level
        print(f'{separator * (trace.recursion_depth + 1)}|-- return {result}')

        return result

    return traced_func


def factorial(n):
   if n == 1: return 1
   else: return n * factorial(n-1)


# since the name of decorated func is the same as the name of original one
# decorator will be applied to nested calls too
factorial = trace(factorial)

# this will print all the recursion trace and the result at the bottom
print(factorial(7))

Would love to hear from you about this solution. Do you like it? Do you have other ideas of how to solve it?

Discover and read more posts from Dmitry Belaventsev
get started
post commentsBe the first to share your opinion
Show more replies