【问题标题】:How to print commands in Python?如何在 Python 中打印命令?
【发布时间】:2013-04-14 20:00:17
【问题描述】:

我不在编程领域,但我最近对 ​​Python 产生了兴趣。我正在编写一些函数,但为了调试,我需要查看正在运行哪些命令。例如:

def foo():
    for i in xrange(0,5):
        a = 1 + i

是否可以让解释器输出

>>> for i in xrange(0,5)
>>> a = 1 + 0
>>> a = 1 + 1
>>> a = 1 + 2
>>> a = 1 + 3
>>> a = 1 + 4

对于

>>> foo()

或者至少写入文件发生了什么?我过去做过一些脚本,我记得这在 DOS 中是可能的,使用 @ECHO ON 或其他东西。我做了一些阅读,我觉得它与 Python 中的标准输入和标准输出有关,所以我尝试了

import sys
def foo():
    for i in xrange(0,5):
        a = 1 + i
        sys.stdin.flush()
        sys.stdout.flush()

但我什么也没得到……我也试过了

import sys
# foo()
sys.stdin.read()
sys.stdout.read()

https://stackoverflow.com/a/3289051/2032568,但它只是挂起。抱歉,如果这不是适合初学者的地方。我找不到任何可以回答我的问题的内容。

【问题讨论】:

  • 您可能认为您的问题很清楚,但在我看来,您下面的 cmets 表明您的问题与您实际提出的问题完全不同。也许你可以编辑你的问题,让它更清楚一点。您是否想要某种实际显示计算表达式所需的所有中间步骤的东西,就像有人在数学课上介绍代数表达式一样?

标签: python debugging io stdout stdin


【解决方案1】:

要让解释器在运行时打印出表达式值,您可以使用print 语句。还要记下 Python 的string formatting facilities

例子:

for i in xrange(0,5):
    a = 1 + i
    # print the value of a:
    print "the current value of variable 'a':", a

没有必要明确地刷新标准输出,除非你想强制打印出没有终止换行符的行:

import sys
import time
for i in xrange(0,5):
    a = 1 + i
    # print the value of a:
    # the trailing comma prevents 'print' from adding a newline
    print "\rthe current value of variable 'a':", a, 
    sys.stdout.flush()
    # short pause for purposes of demonstration
    time.sleep(1)
# finally print a newline
print

要在执行之前打印每个语句,请查看trace 模块。

例子:

y = 0
for xi in range(3):
    y += xi
print y

输出:

$ python -m trace -t tt.py
 --- modulename: tt, funcname: <module>
tt.py(2): y = 0
tt.py(3): for xi in range(3):
tt.py(4):     y += xi
tt.py(3): for xi in range(3):
tt.py(4):     y += xi
tt.py(3): for xi in range(3):
tt.py(4):     y += xi
tt.py(3): for xi in range(3):
tt.py(5): print y
3
 --- modulename: trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)

您首先要寻找的也可能是调试器,例如pdb。您将获得一个交互式会话,您可以在其中单步执行代码,并以交互方式查看数据。

例子:

$ python -m pdb tt.py
> /home/moooeeeep/tt.py(2)<module>()
-> y = 0
(Pdb) n
> /home/moooeeeep/tt.py(3)<module>()
-> for xi in range(3):
(Pdb) n
> /home/moooeeeep/tt.py(4)<module>()
-> y += xi
(Pdb) n
> /home/moooeeeep/tt.py(3)<module>()
-> for xi in range(3):
(Pdb) n
> /home/moooeeeep/tt.py(4)<module>()
-> y += xi
(Pdb) print y, xi
0 1
(Pdb) 
...

大多数 Python IDE(例如,PyDev)都很好地集成了调试功能。所以我的建议是:去买一个调试器。

【讨论】:

    【解决方案2】:

    看看trace-module

    python -m trace --count -C . somefile.py
    

    输出放在currebt目录下:

    $ cat somefile.trace
        1: def foo():
        6:     for i in xrange(5):
        5:         a = 1 + i
    
        1: foo()
    

    -c, --count 在程序完成时生成一组带注释的列表文件,显示每个语句执行了多少次

    如果使用-t 选项,您会得到:

    $ python -m trace --count -t tr.py 
     --- modulename: tr, funcname: <module>
    tr.py(1): def foo():
    tr.py(5): foo()
     --- modulename: tr, funcname: foo
    tr.py(2):     for i in xrange(5):
    tr.py(3):         a = 1 + i
    tr.py(2):     for i in xrange(5):
    tr.py(3):         a = 1 + i
    tr.py(2):     for i in xrange(5):
    tr.py(3):         a = 1 + i
    tr.py(2):     for i in xrange(5):
    tr.py(3):         a = 1 + i
    tr.py(2):     for i in xrange(5):
    tr.py(3):         a = 1 + i
    tr.py(2):     for i in xrange(5):
    

    【讨论】:

      【解决方案3】:

      你的意思是这样的?

      def foo():
               for i in xrange(0,5):
                       a = 1 + i
                       print "a = 1 + {0}".format(i)
      
      >>> foo()
      a = 1 + 0
      a = 1 + 1
      a = 1 + 2
      a = 1 + 3
      a = 1 + 4
      

      【讨论】:

      • 你认为 OP 真的想写明确的打印语句吗?
      【解决方案4】:
      def foo():
          for i in xrange(0,5):
              a = 1 + i
              print a
      

      我认为这就是你要找的东西,我希望这对你有用:)

      编辑:

      我想我明白你想要什么:

      def foo():
          for i in xrange(0,5):
              print "a = 1 + i"
      

      【讨论】:

      • 当然,但是如果我有,比如说:a = (x ** y) + (2 * b),我想得到 a = (-1 ** 2) + (2 * 2),对于 x=-1,y=2,b=2。不仅是结果,这将是 4。
      【解决方案5】:

      我了解您在这里要实现的目标,并且我检查了该链接:它也为我挂起,并且仅在终端中重复输入的文本。不幸的是,我不知道如何按照您的要求打印出脚本:对于调试,我建议只使用简单的打印命令来确定正在执行的部分。

      def foo():
          for i in xrange(0,5):
              a = 1 + i, print "line is being executed where i = %i " % i
      

      然后只需阅读打印的文本输出即可查看程序在做什么。 希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 2012-08-24
        • 1970-01-01
        相关资源
        最近更新 更多