Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print every line of processing code in Python? [closed]

Tags:

python

When testing or creating a new function, I often like to print every line of code that is happening so that I can see how every line is processing.

Is there a way that can serve this purpose? I'm looking for something more convenient so I don't have to type print after every line.

For example, instead of writing this function

def test_func():
    l = range(10)
    print l
    l = zip(l,range(30,40))
    print l 
    l = dict(l)
    print l 

I'd like to write this without writing print, yet still get every line printed

def test_func():
    l = range(10)
    l = zip(l,range(30,40))
    l = dict(l)

Perhaps I can use a Python decorator or something for this?

like image 674
Chris Avatar asked Jul 10 '15 11:07

Chris


People also ask

How do you print full stops in Python?

So, if you want to "add" a period/dot/full stop to the end of your string, you can do this: flavors = ["chocolate", "mint", "strawberry"] ". ".

How do I get the print statement to stop coming to the next line in Python?

It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.

How do you print multiple lines in Python?

"\n" can be used for a new line character, or if you are printing the same thing multiple times then a for loop should be used.


1 Answers

You'd better use debugger for that purpose. But if you want to print each line you can run program with 'trace`.

python -m trace --trace asd.py 

 --- modulename: asd, funcname: <module>
asd.py(1): def test_func():
asd.py(6): test_func();
 --- modulename: asd, funcname: test_func
asd.py(2):     l = range(10)
asd.py(3):     l = zip(l,range(30,40))
asd.py(4):     l = dict(l)
 --- modulename: trace, funcname: _unsettrace
trace.py(80):         sys.settrace(None)
like image 89
deathangel908 Avatar answered Oct 16 '22 09:10

deathangel908