python學習一:軟件版本選擇以及代碼調試初探

    開始學習python了,爭取每天玩一點,寫下點心得。

1. 版本選擇

    官網下載了python 3.3的win安裝程序並安裝,寫了如下簡單的測試程序,想看看idle debugger的功能。

CH_TYPE = "ch"
EN_TYPE = "en"
g_name_type = "ch"  # ch / en
g_first_name = "hang"

def PrintFullName():
    global g_name_type
    global g_first_name
    last_name = "Zhang"

    if g_name_type == CH_TYPE:
        print "full name: %s.%s" % (last_name, g_first_name)


if __name__ == "__main__":
	PrintFullName()
未曾想,在
print "full name: %s.%s" % (last_name, g_first_name)
一行報語法錯誤,search後,發現原來python 2和3有很大差異,從python3.0開始,print由2中的一個statement變爲3中的print()函數,查看3.3的手冊,也學會了print函數的使用。那究竟是使用最新的python3還是使用python2呢?

python的網站上有如下一篇文章:

http://wiki.python.org/moin/Python2orPython3

就目前來看,爲了獲得更多第三方python庫的支持,學習python2會更有利於我。所以我重新安裝了python2.7.5

2. idle debugger

在idle中勾選debugger後,會彈出debug control窗口。

窗口中會顯示stack中當前函數的globals和locals,以及運行到哪一行代碼。

左上角的五個控制按鈕的功能:Go,程序運行完畢;Step,程序運行單行,並進入函數;Over,程序運行單行,但不進入函數內部;Out,程序運行直到當前函數返回;Quit,程序退出。

同時,可以在程序中單擊右鍵設定breakpoint。


3. pdb

python還可以使用pdb這個module來進行調試,在python的手冊中可以索引到pdb模塊的教程。

import pdb

CH_TYPE = "ch"
EN_TYPE = "en"
g_name_type = "ch"  # ch / en
g_first_name = "hang"

def PrintFullName():
    pdb.set_trace()
    global g_name_type
    global g_first_name
    last_name = "Zhang"

    if g_name_type == CH_TYPE:
        print "full name: %s.%s" % (last_name, g_first_name)


if __name__ == "__main__":
	PrintFullName()
代碼中引入pdb,並且在開始debug處調用pdb.set_trace()函數。

這裏對幾個命令進行了學習:

h(elp) [command]查看某個命令的幫助
w(here) 顯示目前的stack信息
d(own) 進入下一個函數棧u(p) 進入上一個函數棧b(reak) [[filename:]lineno | function[, condition]]設定斷點,filename應該在sys.path路徑下tbreak [[filename:]lineno | function[, condition]]設定臨時斷點cl(ear) [filename:lineno | bpnumber [bpnumber ...]]清楚斷點disable [bpnumber [bpnumber ...]]斷點失效enable [bpnumber [bpnumber ...]]斷點生效ignore bpnumber [count]設置斷點失效count次

condition bpnumber [condition] 條件表達式爲真,則斷點生效;表達式爲空,則所有條件被移除

commands [bpnumber]到達斷點時候,執行相應命令,以end爲結尾

An example:

(Pdb) commands 1
(com) print some_variable
(com) end
(Pdb)

To remove all commands from a breakpoint, type commands and follow it immediately with end; that is, give no commands.

With no bpnumber argument, commands refers to the last breakpoint set.


s(tep) Execute the current line, stop at the first possible occasion (either in a function that is called or on the next line in the current function).n(ext) Continue execution until the next line in the current function is reached or it returns. (The difference betweennext andstep is thatstep stops inside a called function, whilenext executes called functions at (nearly) full speed, only stopping at the next line in the current function.)unt(il)

Continue execution until the line with the line number greater than the current one is reached or when returning from current frame.

New in version 2.6.

r(eturn) Continue execution until the current function returns.c(ont(inue)) Continue execution, only stop when a breakpoint is encountered.j(ump) lineno

Set the next line that will be executed. Only available in the bottom-most frame. This lets you jump back and execute code again, or jump forward to skip code that you don’t want to run.

It should be noted that not all jumps are allowed — for instance it is not possible to jump into the middle of afor loop or out of afinally clause.

l(ist) [first[, last]] List source code for the current file. Without arguments, list 11 lines around the current line or continue the previous listing. With one argument, list 11 lines around at that line. With two arguments, list the given range; if the second argument is less than the first, it is interpreted as a count. a(rgs) Print the argument list of the current function.p expression

Evaluate the expression in the current context and print its value.

Note

print can also be used, but is not a debugger command — this executes the Pythonprint statement.

pp expression Like the p command, except the value of the expression is pretty-printed using thepprint module.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章