《Python核心編程》第二版第308頁第十一章練習 續五 -Python核心編程答案-自己做的-

來源:互聯網
上載者:User

本部落格列出的答案不是來自官方資源,是我自己做的練習,如果有疑問或者錯誤,歡迎討論。

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核心編程答案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.