本部落格列出的答案不是來自官方資源,是我自己做的練習,如果有疑問或者錯誤,歡迎討論。
11-18.
同步化函數調用。複習一下第6章中當引入淺拷貝和深拷貝的時候,提到的丈夫和妻子情形(6.20小結)。他們共用了一個普通賬戶,同時對他們銀行賬戶訪問時會發生不利影響。建立一個程式,讓調用改變賬戶收支的函數必須同步。
【未完】
目前感覺本題有難度,暫時押後。
11-19.
Variable Scope. Earlier in the chapter (see Example 11.9 on p. 466), we left determining the output of scope.py as an exercise for the reader.
(a) Write down your best guess, then download the code and run it. Were you close? Explain where you were wrong and why (if applicable). Hint: first determine the total number of lines output by counting how many print statements will execute before trying to figure out the output.
(b) Line 11 in proc2() is currently blank... insert this statement (indented properly): global j. How does this affect the output (and why)?
【注】英文版原書有本題。Example 11.9是指中文版書304頁11.8.6小節的那個例子。
【答案】
(a)運行結果
j == 3 and k == 4j == 1 and k == 7j == 3 and k == 4j == 6 and k == 7j == 8 and k == 7
(b)運行結果
j == 3 and k == 4j == 1 and k == 7j == 3 and k == 4j == 6 and k == 7j == 6 and k == 7
#本文來自部落格園balian
*** *** *** ***
【例子源碼】
Example 11.9 Variable Scope (scope.py)
j, k = 1, 2def proc1(): j, k = 3, 4 print "j == %d and k == %d" % (j, k) k = 5def proc2(): #global j j = 6 proc1() print "j == %d and k == %d" % (j, k)k = 7proc1()print "j == %d and k == %d" % (j, k)j = 8proc2()print "j == %d and k == %d" % (j, k)
*** *** *** ***
keyword Python核心編程答案