How to get the caller class name inside a function of another class in python?

前端 未结 6 796
花落未央
花落未央 2020-12-02 19:08

My objective is to stimulate a sequence diagram of an application for this I need the information about a caller and callee class names at runtime. I can successfully retrie

6条回答
  •  隐瞒了意图╮
    2020-12-02 19:19

    Instead of indexing the return value of inspect.stack(), one could use the method inspect.currentframe(), which avoids the indexing.

    prev_frame = inspect.currentframe().f_back
    the_class = prev_frame.f_locals["self"].__class__
    the_method = prev_frame.f_code.co_name
    

提交回复
热议问题