How To Print Commands In Python?
Solution 1:
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 newlineprint"\rthe current value of variable 'a':", a,
sys.stdout.flush()
# short pause for purposes of demonstration
time.sleep(1)
# finally print a newlineprint
To print each statement before it's executed have a look at the trace module.
Example:
y = 0for xi in range(3):
y += xi
print y
The output:
$ python -m trace -t tt.py
--- modulename: tt, funcname: <module>
tt.py(2): y = 0tt.py(3): for xi in range(3):tt.py(4): y += xitt.py(3): for xi in range(3):tt.py(4): y += xitt.py(3): for xi in range(3):tt.py(4): y += xitt.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.
Solution 2:
Have a look at the trace-module
python -m trace --count -C . somefile.py
output is placed in currebt directory:
$catsomefile.trace1:deffoo():6:foriinxrange(5):5:a=1+i1:foo()
-c, --count Produce a set of annotated listing files upon program completion that shows how many times each statement was executed
If using the -t
option you get this:
$ 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 + itr.py(2): for i in xrange(5):tr.py(3): a = 1 + itr.py(2): for i in xrange(5):tr.py(3): a = 1 + itr.py(2): for i in xrange(5):tr.py(3): a = 1 + itr.py(2): for i in xrange(5):tr.py(3): a = 1 + itr.py(2): for i in xrange(5):
Solution 3:
You mean something like this?
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
Solution 4:
def foo():
for i in xrange(0,5):
a =1 + i
print a
i think this its what you are looking for, i hope this be usefull for you :)
edit:
i think i understood what you want:
def foo():
for i in xrange(0,5):
print "a = 1 + i"
Solution 5:
I understand what you what to achieve here and I checked that link: it hangs for me too, and only repeats inputted text in the Terminal. Unfortunately I don't know how to print out the script as you've asked: for debugging, I'd advise just using simple print commands to work out which section is being executed.
def foo():
for i in xrange(0,5):
a =1 + i, print "line is being executed where i = %i " % i
Then just read the printed text output to see what the program is doing. Hope this helps.
Post a Comment for "How To Print Commands In Python?"