Một trong những tính năng Python khó hiểu nhất đối với người mới bắt đầu là câu lệnh "with", liên quan đến trình quản lý ngữ cảnh vì không phải ngôn ngữ lập trình nào cũng có cú pháp này
Quản lý tài nguyên. Trong bất kỳ ngôn ngữ lập trình nào, việc sử dụng các tài nguyên như thao tác tệp hoặc kết nối cơ sở dữ liệu là rất phổ biến. Nhưng những tài nguyên này bị hạn chế về nguồn cung. Do đó, vấn đề chính nằm ở việc đảm bảo giải phóng các tài nguyên này sau khi sử dụng. Nếu chúng không được giải phóng thì sẽ dẫn đến rò rỉ tài nguyên và có thể khiến hệ thống chạy chậm lại hoặc gặp sự cố. Sẽ rất hữu ích nếu người dùng có cơ chế tự động thiết lập và chia nhỏ tài nguyên. Trong Python, điều này có thể đạt được bằng cách sử dụng các trình quản lý bối cảnh tạo điều kiện thuận lợi cho việc xử lý tài nguyên hợp lý. Cách phổ biến nhất để thực hiện các thao tác với tệp là sử dụng từ khóa như hình bên dưới.
Python3
init method called enter method called with statement block exit method called6
init method called enter method called with statement block exit method called7
_______48____49____50
init method called enter method called with statement block exit method called1____52
init method called enter method called with statement block exit method called0
init method called enter method called with statement block exit method called1
Hãy lấy ví dụ về quản lý tệp. Khi một tệp được mở, một bộ mô tả tệp được sử dụng, đây là một tài nguyên hạn chế. Tại một thời điểm, chỉ một số tệp nhất định có thể được mở bởi một quy trình. Chương trình sau đây chứng minh điều đó.
Python3
init method called enter method called with statement block exit method called2
init method called enter method called with statement block exit method called0
init method called enter method called with statement block exit method called4
init method called enter method called with statement block exit method called5
init method called enter method called with statement block exit method called6
init method called enter method called with statement block exit method called7
init method called enter method called with statement block exit method called8
init method called enter method called with statement block exit method called9
init method called enter method called with statement block exit method called00
init method called enter method called with statement block exit method called01
init method called enter method called with statement block exit method called1
init method called enter method called with statement block exit method called03____49
init method called enter method called with statement block exit method called9
init method called enter method called with statement block exit method called06
init method called enter method called with statement block exit method called07
init method called enter method called with statement block exit method called08
init method called enter method called with statement block exit method called09
đầu ra
init method called enter method called with statement block exit method called8
Thông báo lỗi nói rằng có quá nhiều tệp đang mở. Ví dụ trên là trường hợp rò rỉ bộ mô tả tệp. Nó xảy ra vì có quá nhiều tệp đang mở và chúng không được đóng. Có thể có khả năng một lập trình viên có thể quên đóng một tệp đã mở.
Quản lý tài nguyên bằng trình quản lý ngữ cảnh. Giả sử một khối mã phát sinh một ngoại lệ hoặc nếu nó có một thuật toán phức tạp với nhiều đường dẫn trả về, thì việc đóng một tệp ở tất cả các vị trí sẽ trở nên khó khăn. Nói chung, trong các ngôn ngữ khác khi làm việc với tệp, try-ngoại trừ cuối cùng được sử dụng để đảm bảo rằng tài nguyên tệp được đóng sau khi sử dụng ngay cả khi có ngoại lệ. Python cung cấp một cách dễ dàng để quản lý tài nguyên. Trình quản lý bối cảnh. Từ khóa with được sử dụng. Khi nó được đánh giá, nó sẽ dẫn đến một đối tượng thực hiện quản lý bối cảnh. Trình quản lý bối cảnh có thể được viết bằng các lớp hoặc hàm [có trang trí]
Tạo Trình quản lý ngữ cảnh. Khi tạo trình quản lý ngữ cảnh bằng các lớp, người dùng cần đảm bảo rằng lớp đó có các phương thức. __enter__[] và __exit__[]. __enter__[] trả về tài nguyên cần được quản lý và __exit__[] không trả về gì ngoài thực hiện các hoạt động dọn dẹp. Đầu tiên, chúng ta hãy tạo một lớp đơn giản gọi là ContextManager để hiểu cấu trúc cơ bản của việc tạo trình quản lý ngữ cảnh bằng cách sử dụng các lớp, như minh họa bên dưới.
Python3
init method called enter method called with statement block exit method called60
init method called enter method called with statement block exit method called61
init method called enter method called with statement block exit method called62
init method called enter method called with statement block exit method called63
init method called enter method called with statement block exit method called1
init method called enter method called with statement block exit method called65
init method called enter method called with statement block exit method called66
init method called enter method called with statement block exit method called67
init method called enter method called with statement block exit method called01
init method called enter method called with statement block exit method called69
init method called enter method called with statement block exit method called70____09
init method called enter method called with statement block exit method called72
init method called enter method called with statement block exit method called73
init method called enter method called with statement block exit method called69
init method called enter method called with statement block exit method called1
init method called enter method called with statement block exit method called65
init method called enter method called with statement block exit method called77
init method called enter method called with statement block exit method called67
init method called enter method called with statement block exit method called01
init method called enter method called with statement block exit method called69
init method called enter method called with statement block exit method called70____09
init method called enter method called with statement block exit method called83
init method called enter method called with statement block exit method called73
init method called enter method called with statement block exit method called69
init method called enter method called with statement block exit method called86
init method called enter method called with statement block exit method called67
init method called enter method called with statement block exit method called1
init method called enter method called with statement block exit method called1____465
init method called enter method called with statement block exit method called91____467
init method called enter method called with statement block exit method called93
init method called enter method called with statement block exit method called69
init method called enter method called with statement block exit method called70____09
init method called enter method called with statement block exit method called97
init method called enter method called with statement block exit method called73
init method called enter method called with statement block exit method called99
init method called enter method called with statement block exit method called1____470____09
init method called enter method called with statement block exit method called03
init method called enter method called with statement block exit method called73
đầu ra
init method called enter method called with statement block exit method called
Trong trường hợp này, một đối tượng ContextManager được tạo. Điều này được gán cho biến sau từ khóa i. quản lý điện tử. Khi chạy chương trình trên, phần sau được thực hiện theo trình tự
- __trong đó__[]
- __đi vào__[]
- thân câu lệnh [mã bên trong khối with]
- __exit__[][các tham số trong phương thức này được sử dụng để quản lý các ngoại lệ]
Quản lý tệp bằng trình quản lý bối cảnh. Hãy áp dụng khái niệm trên để tạo một lớp giúp quản lý tài nguyên tệp. Lớp Trình quản lý tệp giúp mở tệp, ghi/đọc nội dung và sau đó đóng tệp.
Python3
init method called enter method called with statement block exit method called6
init method called enter method called with statement block exit method called06
init method called enter method called with statement block exit method called61
init method called enter method called with statement block exit method called62
init method called enter method called with statement block exit method called09
init method called enter method called with statement block exit method called1
init method called enter method called with statement block exit method called65
init method called enter method called with statement block exit method called66
init method called enter method called with statement block exit method called67
init method called enter method called with statement block exit method called14
init method called enter method called with statement block exit method called69
init method called enter method called with statement block exit method called67____517
init method called enter method called with statement block exit method called0
init method called enter method called with statement block exit method called19
init method called enter method called with statement block exit method called69
init method called enter method called with statement block exit method called67____522
init method called enter method called with statement block exit method called0
init method called enter method called with statement block exit method called24
init method called enter method called with statement block exit method called69
init method called enter method called with statement block exit method called67____527
init method called enter method called with statement block exit method called28
init method called enter method called with statement block exit method called0
init method called enter method called with statement block exit method called00
init method called enter method called with statement block exit method called69
init method called enter method called with statement block exit method called1
init method called enter method called with statement block exit method called65
init method called enter method called with statement block exit method called77
init method called enter method called with statement block exit method called67
init method called enter method called with statement block exit method called01
init method called enter method called with statement block exit method called69
init method called enter method called with statement block exit method called67____527
init method called enter method called with statement block exit method called28
init method called enter method called with statement block exit method called0
init method called enter method called with statement block exit method called9
init method called enter method called with statement block exit method called9
init method called enter method called with statement block exit method called67
init method called enter method called with statement block exit method called15
init method called enter method called with statement block exit method called67
init method called enter method called with statement block exit method called17
init method called enter method called with statement block exit method called69
init method called enter method called with statement block exit method called86
init method called enter method called with statement block exit method called67
init method called enter method called with statement block exit method called27
init method called enter method called with statement block exit method called28
init method called enter method called with statement block exit method called1
init method called enter method called with statement block exit method called1____465
init method called enter method called with statement block exit method called91____467
init method called enter method called with statement block exit method called93
init method called enter method called with statement block exit method called69____467____527
init method called enter method called with statement block exit method called28
init method called enter method called with statement block exit method called33
init method called enter method called with statement block exit method called34
init method called enter method called with statement block exit method called35
init method called enter method called with statement block exit method called06____107
init method called enter method called with statement block exit method called08
init method called enter method called with statement block exit method called39
_______51____041____042
init method called enter method called with statement block exit method called73
init method called enter method called with statement block exit method called70
init method called enter method called with statement block exit method called45
đầu ra
init method called enter method called with statement block exit method called0
Quản lý tệp bằng trình quản lý ngữ cảnh và có câu lệnh. Khi thực hiện khối with, các hoạt động sau đây sẽ diễn ra theo trình tự
- Một đối tượng FileManager được tạo bằng test. txt làm tên tệp và w[write] làm chế độ khi phương thức __init__ được thực thi
- Phương thức __enter__ mở bài kiểm tra. txt ở chế độ ghi [thao tác thiết lập] và trả về một đối tượng tệp cho biến f
- Văn bản 'Kiểm tra' được ghi vào tệp
- Phương thức __exit__ đảm nhiệm việc đóng tệp khi thoát khỏi khối with [thao tác xé nhỏ]. Khi in[f. đã đóng] được chạy, đầu ra là True vì Trình quản lý tệp đã đảm nhiệm việc đóng tệp mà nếu không thì cần phải thực hiện rõ ràng
Quản lý kết nối cơ sở dữ liệu bằng trình quản lý bối cảnh và với câu lệnh. Khi thực hiện khối with, các hoạt động sau đây sẽ diễn ra theo trình tự