Python

Python debug 神器之一

Never use print for debugging again 真的講得很好~

新手最土炮的方法就是在問題之前 print 函式

有幸可以不再使用 print 真的是很幸福的事

來看看範例

import pysnooper

@pysnooper.snoop()
def dict_for2():
    dict1 = {
        'a': 1,
        'b': 2,
        'c': 3
    }

    for k, v in dict1.items():
        print("key is : {}".format(k))
        print("value is : {}".format(v))

執行後顯示

Source path:... U:/PY/practice/for_loop.py
23:32:44.931199 call        40 def dict_for2():
23:32:44.932198 line        42         'a': 1,
23:32:44.932198 line        43         'b': 2,
23:32:44.932198 line        44         'c': 3
23:32:44.932198 line        41     dict1 = {
New var:....... dict1 = {'a': 1, 'b': 2, 'c': 3}
23:32:44.932198 line        47     for k, v in dict1.items():
New var:....... k = 'a'
New var:....... v = 1
23:32:44.932198 line        48         print("key is : {}".format(k))
23:32:44.932198 line        49         print("value is : {}".format(v))
23:32:44.932198 line        47     for k, v in dict1.items():
Modified var:.. k = 'b'
Modified var:.. v = 2
23:32:44.932198 line        48         print("key is : {}".format(k))
23:32:44.932198 line        49         print("value is : {}".format(v))
23:32:44.932198 line        47     for k, v in dict1.items():
Modified var:.. k = 'c'
Modified var:.. v = 3
23:32:44.932198 line        48         print("key is : {}".format(k))
23:32:44.932198 line        49         print("value is : {}".format(v))
23:32:44.932198 line        47     for k, v in dict1.items():
23:32:44.932198 return      47     for k, v in dict1.items():
Return value:.. None
Elapsed time: 00:00:00.000999
key is : a
value is : 1
key is : b
value is : 2
key is : c
value is : 3

還有給 Elapsed time

其實有點像 shell script 的 -x 方法

真心推薦~

comments powered by Disqus