[Python] python call stacks and decorators

Discussão em 'Python' iniciado por Stack, Maio 9, 2025.

  1. Stack

    Stack Membro Participativo

    Suppose I have code below:

    @contextmanager
    def my_context(name):
    print(f"[ENTER] Context: {name}")
    try:
    yield
    finally:
    print(f"[EXIT] Context: {name}")
    print()

    def c():
    print("c")

    def b():
    c()
    print('b')

    @print_call_stack_decorator
    def a():
    b()
    print('a')

    a()


    how can I create the print_call_stack_decorator, and use it on the top function a, such that I can print out below (basically decorating my_context on each subfunction along the call stack with one big decorator print_call_stack_decorator)

    [ENTER] Context: a
    [ENTER] Context: b
    [ENTER] Context: c
    c
    [EXIT] Context: c

    b
    [EXIT] Context: b

    a
    [EXIT] Context: a

    Continue reading...

Compartilhe esta Página