Câu hỏi được gắn thẻ «with-statement»


6
Nhiều biến trong câu lệnh 'với'?
Có thể khai báo nhiều hơn một biến bằng cách sử dụng một withcâu lệnh trong Python không? Cái gì đó như: from __future__ import with_statement with open("out.txt","wt"), open("in.txt") as file_out, file_in: for line in file_in: file_out.write(line) ... hoặc là làm sạch hai tài nguyên cùng một lúc vấn đề?


6
Giải thích '__enter__' của Python và '__exit__'
Tôi đã thấy điều này trong mã của ai đó. Nó có nghĩa là gì? def __enter__(self): return self def __exit__(self, type, value, tb): self.stream.close() from __future__ import with_statement#for python2.5 class a(object): def __enter__(self): print 'sss' return 'sss111' def __exit__(self ,type, value, traceback): print 'ok' return False with a() as …





Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookie và Chính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.