为什么要“设置个人资料”打印这个

发布于 2024-08-17 08:42:49 字数 425 浏览 5 评论 0原文

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()

print

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路黄昏 2024-08-24 08:42:49

profiler 函数在每次分析事件时都会被调用,因为您对其调用了 sys.setprofile

每次调用它时,它都会打印一行,因为您将无条件 print 语句作为其主体。我们很难告诉您为什么您这样做,这使得您的“为什么”问题变得非常非常奇怪。

根据文档:

<前><代码>'呼叫'

调用一个函数(或其他一些函数)
输入代码块)。

<前><代码>'返回'

函数(或其他代码块)是
即将返回。

<前><代码>'c_call'

即将调用 AC 函数。
这可能是一个扩展函数或
内置。

<前><代码>'c_return'

AC功能已返回。

以下是我在稍微简单、清晰的情况下观察到的情况(Python 2.5 或 2.6、MacOSX):

>>> def a():
...     print 'aaa'
... 
>>> def profiler(frame, event, arg):
...     print 'PROF %r %r' % (event, arg)
... 
>>> sys.setprofile(profiler)
PROF 'return' None
>>> a()
PROF 'call' None
PROF 'c_call' <built-in function utf_8_decode>
PROF 'c_return' <built-in function utf_8_decode>
PROF 'return' (u'a()\n', 4)
PROF 'call' None
PROF 'call' None
aaa
PROF 'return' None
PROF 'return' None

不确定为什么看不到 c_callc_return< /code> 情况如你所愿——也许在你的特定平台(什么操作系统?什么级别的 Python?什么 IDE(如果有))中没有用于打印的隐式 utf-8 转换。

The profiler function gets called at each profiling event because you called sys.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:

'call'

A function is called (or some other
code block entered).

'return'

A function (or other code block) is
about to return.

'c_call'

A C function is about to be called.
This may be an extension function or a
built-in.

'c_return'

A C function has returned.

Here's what I observe (Python 2.5 or 2.6, MacOSX) in a slightly simpler, sharper case:

>>> def a():
...     print 'aaa'
... 
>>> def profiler(frame, event, arg):
...     print 'PROF %r %r' % (event, arg)
... 
>>> sys.setprofile(profiler)
PROF 'return' None
>>> a()
PROF 'call' None
PROF 'c_call' <built-in function utf_8_decode>
PROF 'c_return' <built-in function utf_8_decode>
PROF 'return' (u'a()\n', 4)
PROF 'call' None
PROF 'call' None
aaa
PROF 'return' None
PROF 'return' None

Not sure why you don't see the c_call and c_return cases as you should -- maybe there is no implicit utf-8 conversion for printing in your specific platform (what OS? what level of Python? what IDE if any).

◇流星雨 2024-08-24 08:42:49

您似乎想知道为什么 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文