Sobes.tech
Junior — Senior

Decorator that outputs a message when calling a method

livecode

Task condition

It is necessary to implement a decorator @trace that, upon each call of any class method, outputs a string like "Entering function <method_name>". For example, when calling the method speak(), a message "Entering function speak" should appear, and when calling the method walk(), "Entering function walk" should appear.

class Person:
    def __init__(self, name):
        self.name = name
        self.distance = 0

    @trace
    def speak(self, sentence):
        print(f'{self.name}: "{sentence}"')

    @trace
    def walk(self, distance):
        self.distance += distance
        print(f'{self.name}: total distance is {self.distance} meters')