我如何打印称为函数的函数 [英] How do I print functions as they are called

查看:67
本文介绍了我如何打印称为函数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调试Python脚本时,我真的很想知道整个程序的整个调用堆栈。理想的情况是,如果有一个用于python的命令行标志,它将导致Python在调用所有函数名时打印它们(我检查了 man Python2.7 ,但是

In debugging a Python script, I'd really like to know the entire call stack for my entire program. An ideal situation would be if there were a command-line flag for python that would cause Python to print all function names as they are called (I checked man Python2.7, but didn't find anything of this sort).

由于此脚本中的函数数量众多,我宁愿不要在每个函数的开头添加打印语句函数和/或类(如果可能)。

Because of the number of functions in this script, I'd prefer not to add a print statement to the beginning of each function and/or class, if possible.

一种中间解决方案是使用PyDev的调试器,放置几个断点并检查程序中给定点的调用堆栈,因此,我暂时将使用这种方法。

An intermediate solution would be to use PyDev's debugger, place a couple breakpoints and check the call stack for given points in my program, so I'll use this approach for the time being.

我仍然希望查看程序整个生命周期内调用的所有函数的完整列表。存在一个方法。

I'd still prefer to see a complete list of all functions called throughout the life of the program, if such a method exists.

推荐答案

您可以使用跟踪函数来完成此操作(Spacedman的道具,用于改进此方法的原始版本以进行跟踪)返回并使用一些不错的缩进):

You can do this with a trace function (props to Spacedman for improving the original version of this to trace returns and use some nice indenting):

def tracefunc(frame, event, arg, indent=[0]):
      if event == "call":
          indent[0] += 2
          print("-" * indent[0] + "> call function", frame.f_code.co_name)
      elif event == "return":
          print("<" + "-" * indent[0], "exit function", frame.f_code.co_name)
          indent[0] -= 2
      return tracefunc

import sys
sys.setprofile(tracefunc)

main()   # or whatever kicks off your script

请注意,函数的代码对象通常与关联的函数,但并非总是如此,因为函数可以动态创建。不幸的是,Python并没有跟踪堆栈上的函数对象(我有时幻想为此提交补丁)。但是,这肯定足够好在大多数情况下。

Note that a function's code object usually has the same name as the associated function, but not always, since functions can be created dynamically. Unfortunately, Python doesn't track the function objects on the stack (I've sometimes fantasized about submitting a patch for this). Still, this is certainly "good enough" in most cases.

如果这成为问题,则可以从源代码中提取真实函数名称-Python会跟踪文件名和行号-或询问垃圾收集器找出哪个功能对象引用了代码对象。可能有多个功能共享该代码对象,

If this becomes an issue, you could extract the "real" function name from the source code—Python does track the filename and line number—or ask the garbage collector find out which function object refers to the code object. There could be more than one function sharing the code object, but any of their names might be good enough.

四年后再来回顾一下,我应该提到在Python 2.6和之后,您可以使用 sys.setprofile()而不是 sys.settrace()获得更好的性能。可以使用相同的跟踪功能。只是配置文件功能仅在进入或退出某个功能时才被调用,因此该功能内部的内容会全速执行。

Coming back to revisit this four years later, it behooves me to mention that in Python 2.6 and later, you can get better performance by using sys.setprofile() rather than sys.settrace(). The same trace function can be used; it's just that the profile function is called only when a function is entered or exited, so what's inside the function executes at full speed.

这篇关于我如何打印称为函数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆