Wednesday, 30 March 2022

Debug function call

I came across a useful python debugging library the other day that allows you to print where a function is being called from: 

from inspect import stack

def foo():
    print(stack()[1][1:4])

def bar():
    foo()
What this will do now is whenever foo() is called, it will also now spit out a message:
('name_of_file.py', line_number, function_calling_foo)
 
Or, as I call it in Maya's Script Editor:
('<maya console>', 6, 'bar') 

Brilliant for tracing nested funcs in a complex architecture!

1 comment:

  1. from inspect import stack
    print(stack()[1][1:4])

    ReplyDelete