tailieunhanh - Advance Praise for Head First Python Part 4

Nếu không có lỗi runtime xảy ra, bất kỳ mã trong bộ phần mềm cuối cùng thực hiện. Tương tự, nếu IOError xảy ra, bộ ứng dụng ngoại trừ thực hiện và sau đó các bộ cuối cùng chạy. Không có vấn đề gì, các mã trong bộ phần mềm cuối cùng luôn luôn chạy. | persistence Extend try with finally When you have a situation where code must always run no matter what errors occur add that code to your try statement s finally suite No thanes here e ồeyt the ialls -to eloseO are woved -to here. try man_file open w other_file open w print man file man_file print other file other_file except lOError print File error. finally If no runtime errors occur any code in the finally suite executes. Equally if an lOError occurs the except suite executes and then the finally suite runs. No matter what the code in the finally suite always runs. By moving your file closing code into your finally suite you are reducing the possibility of data corruption errors. This is a big improvement because you re now ensuring that files are closed properly even when write errors occur . But what about those errors How do you find out the specifics of the error you are here 4 115 no dumb questiohns diereiarenQ Dumb Questions 0 I m intrigued. When you stripped the line_spoken data of unwanted whitespace you assigned the result back to the line_spoken variable. Surely invoking the strip method on line_spoken changed the string it refers to A. No that s not what happens. Strings in Python are immutable which means that once a string is created it cannot be changed. 0 But you did change the line_spoken string by removing any unwanted whitespace right A. Yes and no. What actually happens is that invoking the strip method on the line_spoken string creates a new string with leading and trailing whitespace removed. The new string is then assigned to line_spoken replacing the data that was referred to before. In effect it is as if you changed line_ spoken when you ve actually completely replaced the data it refers to. 0 So what happens to the replaced data A Python s built-in memory management technology reclaims the RAM it was using and makes it available to your program. That is unless some other Python