python 打印命令,如何在Python中打印命令?

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:

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

-> y = 0

(Pdb) n

> /home/moooeeeep/tt.py(3)()

-> for xi in range(3):

(Pdb) n

> /home/moooeeeep/tt.py(4)()

-> y += xi

(Pdb) n

> /home/moooeeeep/tt.py(3)()

-> for xi in range(3):

(Pdb) n

> /home/moooeeeep/tt.py(4)()

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值