The answer in this blog is not from official resources, but from my own exercises. If you have any questions or errors, please discuss them.
11-18.
Synchronous function call. Review the situation of the husband and wife mentioned when introducing the shortest copy and deep copy in chapter 1 (Conclusion 6th ). They share a common account and adversely affect access to their bank account. Create a program so that the function called to change the account balance must be synchronized.
[Unfinished]
It is difficult to answer this question.
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 statements 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 )?
[Note] the original English version has this question. Example 11.9 refers to the Example in section 11.8.6 of the Chinese version.
[Answer]
(A) running result
j == 3 and k == 4j == 1 and k == 7j == 3 and k == 4j == 6 and k == 7j == 8 and k == 7
(B) running result
j == 3 and k == 4j == 1 and k == 7j == 3 and k == 4j == 6 and k == 7j == 6 and k == 7
# This article is from balian
*** *** *** ***
[Example source code]
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)
*** *** *** ***
Key word Python core programming answers