如何在Python中打印命令? [英] How to print commands in Python?

查看:186
本文介绍了如何在Python中打印命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不在编程区,但我最近对Python感兴趣。我正在编写一些功能,但是为了调试,我需要看看正在运行的命令。例如:

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

是否可以使解释器输出

 >>>对于我在xrange(0,5)
>>>> a = 1 + 0
>>>> a = 1 + 1
>>>> a = 1 + 2
>>>> a = 1 + 3
>>>> a = 1 + 4

对于

 >>> foo()

或至少写一个文件发生了什么?我以前做了一些脚本,我记得在DOS中可以使用@ECHO ON或某些东西。我做了一些阅读,我觉得它与Python中的stdin和stdout有关,所以我试过

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

但是我没有收到...我也尝试过

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

http://stackoverflow.com / a / 3289051/2032568 ,但它只是挂起来。对不起,如果这不是初学者的正确的地方。

解决方案

要使解释器在运行时打印出表达式值,您可以使用打印声明。还要记下Python的字符串格式化设施



示例:

  for x in xrange(0,5):
a = 1 + i
#打印值a:
print变量'a'的当前值:a

没有必要明确冲洗stdout,除非你想强制打印没有终止换行符的行:

  import sys 
导入时间
在xrange(0,5)中:
a = 1 + i
#打印a的值:
#尾随逗号防止打印'添加一个换行符
打印\r'变量'a'的当前值:a,
sys.stdout.flush()
#短暂暂停用于演示
time.sleep(1)
#终于打印换行
打印






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



示例:

  y = 0 
for xi in range(3):
y + = xi
打印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(无)






您首先要查找的内容,也可能是调试器,例如 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 )都有很好的集成调试功能。所以我的建议:去找一个调试器。


I'm not in the programming area but I recently got interested in Python. I was writing some functions but for debugging I need to see what commands are running. For instance:

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

Is it possible to make the interpreter output

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

For

>>> foo()

Or at least write to a file what is happening? I did some scripting in the past and I remember that this was possible in DOS, using @ECHO ON or something. I did some reading and I feel like it's related to stdin and stdout in Python so I tried

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

But I get nothing... I also tried

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

and http://stackoverflow.com/a/3289051/2032568, but it just hangs. Sorry if this is not the right place for beginners. I couldn't find anything that answers my question.

解决方案

To make the interpreter print out expression values during runtime you can use the print statement. Also take a note of Python's string formatting facilities.

Example:

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

There is no need to flush stdout explicitly, unless you want to enforce to print out lines without a terminating newline:

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


To print each statement before it's executed have a look at the trace module.

Example:

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

The output:

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


What you are looking for in the first place, might also be a debugger, e.g. pdb. You get an interactive session, where you can step through the code, and have a look at the data interactively.

Example:

$ 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) 
...

Most Python IDEs (e.g., PyDev) have nicely integrated debugging functionality. So my suggestion: go and get a debugger.

这篇关于如何在Python中打印命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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