为什么要“设置个人资料”打印这个
import sys
def a():
print 'aaa'
def profiler(frame, event, arg):
print event, frame.f_code.co_name, frame.f_lineno, "->", arg
# profiler is activated on the next call, return, or exception
sys.setprofile(profiler)
a()
打印
call a 5 -> None#what is it
aaa
return a 6 -> None#what is it
return <module> 12 -> None#what is it
为什么打印这个。
import sys
def a():
print 'aaa'
def profiler(frame, event, arg):
print event, frame.f_code.co_name, frame.f_lineno, "->", arg
# profiler is activated on the next call, return, or exception
sys.setprofile(profiler)
a()
call a 5 -> None#what is it
aaa
return a 6 -> None#what is it
return <module> 12 -> None#what is it
why print this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
profiler
函数在每次分析事件时都会被调用,因为您对其调用了sys.setprofile
。每次调用它时,它都会打印一行,因为您将无条件
print
语句作为其主体。我们很难告诉您为什么您这样做,这使得您的“为什么”问题变得非常非常奇怪。根据文档:
以下是我在稍微简单、清晰的情况下观察到的情况(Python 2.5 或 2.6、MacOSX):
不确定为什么看不到
c_call
和c_return< /code> 情况如你所愿——也许在你的特定平台(什么操作系统?什么级别的 Python?什么 IDE(如果有))中没有用于打印的隐式
utf-8
转换。The
profiler
function gets called at each profiling event because you calledsys.setprofile
on it.Each time it's called, it prints a line, because you put an unconditional
print
statement as its body. Why you did that, is hard for us to tell you, making your "why" questions really, truly peculiar.Profiling events are just calls and returns, per the docs:
Here's what I observe (Python 2.5 or 2.6, MacOSX) in a slightly simpler, sharper case:
Not sure why you don't see the
c_call
andc_return
cases as you should -- maybe there is no implicitutf-8
conversion for printing in your specific platform (what OS? what level of Python? what IDE if any).您似乎想知道为什么 arg 是 None 。
arg
对于每个事件都有不同的含义。对于“return”,arg
是要返回的值。对于“异常”,它是异常信息的三元组。请参阅 http://docs.python.org/library/sys.html#sys .settrace 了解更多信息。It seems like maybe you're wondering why arg is None.
arg
has different meanings for each event. For "return",arg
is the value to be returned. For "exception", it's a triple of exception information. See http://docs.python.org/library/sys.html#sys.settrace for more information.