{"id":97163,"date":"2026-08-29T04:57:08","date_gmt":"2026-08-28T20:57:08","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/97163.html"},"modified":"2026-08-29T04:57:08","modified_gmt":"2026-08-28T20:57:08","slug":"%e4%bd%a0%e7%9a%84%e5%a4%9a%e4%b8%aa-with-%e5%b5%8c%e5%a5%97%e4%b8%ba%e4%bd%95%e5%83%8f%e5%a5%97%e5%a8%83%e5%9c%b0%e7%8b%b1%ef%bc%9f-python-contextlib-exitstack","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/97163.html","title":{"rendered":"\u4f60\u7684\u591a\u4e2a with \u5d4c\u5957\u4e3a\u4f55\u50cf\u201c\u5957\u5a03\u5730\u72f1\u201d\uff1f\u2014\u2014Python contextlib.ExitStack \u7684\u591a\u4e0a\u4e0b\u6587\u7ba1\u7406\u827a\u672f\u4e0e\u81f4\u547d\u9677\u9631"},"content":{"rendered":"<h2>\u4f60\u7684\u591a\u4e2a with \u5d4c\u5957\u4e3a\u4f55\u50cf\u201c\u5957\u5a03\u5730\u72f1\u201d&#xff1f;\u2014\u2014Python contextlib.ExitStack \u7684\u591a\u4e0a\u4e0b\u6587\u7ba1\u7406\u827a\u672f\u4e0e\u81f4\u547d\u9677\u9631<\/h2>\n<hr \/>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260828205701-6a91f61db0d33.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p>\u5728 Python \u4e2d&#xff0c;with \u8bed\u53e5\u662f\u8d44\u6e90\u7ba1\u7406\u7684\u9ec4\u91d1\u6807\u51c6\u3002\u4f46\u5f53\u9700\u8981\u540c\u65f6\u7ba1\u7406\u591a\u4e2a\u8d44\u6e90\u2014\u2014\u6bd4\u5982\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\u3001\u83b7\u53d6\u591a\u628a\u9501\u3001\u5efa\u7acb\u591a\u4e2a\u8fde\u63a5\u2014\u2014\u4f60\u53ef\u80fd\u4f1a\u9677\u5165\u4e00\u79cd\u201c\u5957\u5a03\u5730\u72f1\u201d&#xff1a;with \u4e00\u5c42\u5957\u4e00\u5c42&#xff0c;\u7f29\u8fdb\u8d8a\u6765\u8d8a\u6df1&#xff0c;\u4ee3\u7801\u53ef\u8bfb\u6027\u6025\u5267\u4e0b\u964d\u3002\u66f4\u7cdf\u7684\u662f&#xff0c;\u5982\u679c\u8fd9\u4e9b\u8d44\u6e90\u662f\u52a8\u6001\u6570\u91cf\u7684&#xff08;\u6bd4\u5982\u6839\u636e\u7528\u6237\u8f93\u5165\u6253\u5f00\u4e0d\u540c\u6570\u91cf\u7684\u6587\u4ef6&#xff09;&#xff0c;\u4f60\u751a\u81f3\u65e0\u6cd5\u7528\u9759\u6001\u5d4c\u5957\u7684 with \u6765\u8868\u8fbe\u3002\u4e00\u4e9b\u5f00\u53d1\u8005\u9009\u62e9\u624b\u52a8\u7ba1\u7406\u8d44\u6e90\u5217\u8868&#xff0c;\u7136\u540e\u9010\u4e2a try\/finally \u5173\u95ed&#xff0c;\u8fd9\u53c8\u5bb9\u6613\u9057\u6f0f\u6216\u987a\u5e8f\u9519\u4e71&#xff0c;\u5bfc\u81f4\u8d44\u6e90\u6cc4\u6f0f\u6216\u5f02\u5e38\u63a9\u76d6\u3002<\/p>\n<p>contextlib.ExitStack \u6b63\u662f\u4e3a\u4e86\u89e3\u51b3\u8fd9\u7c7b\u95ee\u9898\u800c\u751f\u7684\u5f3a\u529b\u5de5\u5177\u3002\u5b83\u5141\u8bb8\u4f60\u6ce8\u518c\u4efb\u610f\u6570\u91cf\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6216\u6e05\u7406\u56de\u8c03&#xff0c;\u5e76\u5728\u79bb\u5f00 with \u5757\u65f6\u7edf\u4e00\u3001\u6309\u6b63\u786e\u987a\u5e8f\u9000\u51fa\u3002\u4f46\u5f3a\u5927\u7684\u80fd\u529b\u4e5f\u4f34\u968f\u7740\u590d\u6742\u7684\u884c\u4e3a&#xff0c;\u5f88\u591a\u4eba\u5bf9\u5b83\u7684\u56de\u6eda\u987a\u5e8f\u3001\u5f02\u5e38\u5904\u7406\u3001\u4ee5\u53ca\u5982\u4f55\u517c\u5bb9\u5f02\u6b65\u8d44\u6e90\u611f\u5230\u56f0\u60d1\u3002\u4eca\u5929&#xff0c;\u6211\u4eec\u5c31\u6765\u5f7b\u5e95\u89e3\u5256 ExitStack \u7684\u9b54\u6cd5&#xff0c;\u8ba9\u4f60\u5f7b\u5e95\u6446\u8131\u8d44\u6e90\u7ba1\u7406\u7684\u5669\u68a6\u3002<\/p>\n<h3>\u4e00\u3001\u95ee\u9898\u590d\u73b0&#xff1a;\u5d4c\u5957\u548c\u52a8\u6001\u8d44\u6e90\u7684\u75db\u82e6<\/h3>\n<h4>\u573a\u666f 1&#xff1a;\u56fa\u5b9a\u4f46\u8fc7\u591a\u7684\u5d4c\u5957 with<\/h4>\n<p><span class=\"token keyword\">with<\/span> <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;input.txt&#039;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> fin<span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">with<\/span> <span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;output.txt&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;w&#039;<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> fout<span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">with<\/span> lock_a<span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token keyword\">with<\/span> lock_b<span class=\"token punctuation\">:<\/span><br \/>\n                process<span class=\"token punctuation\">(<\/span>fin<span class=\"token punctuation\">,<\/span> fout<span class=\"token punctuation\">)<\/span><\/p>\n<p>\u56db\u5c42\u7f29\u8fdb\u5df2\u7ecf\u8ba9\u4ee3\u7801\u5f00\u59cb\u201c\u5411\u53f3\u770b\u9f50\u201d\u3002\u5982\u679c\u589e\u52a0\u5230\u4e03\u516b\u4e2a\u8d44\u6e90&#xff0c;\u4ee3\u7801\u5c31\u4f1a\u53d8\u5f97\u6781\u5176\u4e11\u964b&#xff0c;\u4e14\u5bb9\u6613\u56e0\u4e3a\u7f29\u8fdb\u9519\u8bef\u800c\u5f71\u54cd\u903b\u8f91\u3002<\/p>\n<h4>\u573a\u666f 2&#xff1a;\u8d44\u6e90\u6570\u91cf\u52a8\u6001\u53d8\u5316<\/h4>\n<p>\u4f60\u6709\u4e00\u4e2a\u51fd\u6570&#xff0c;\u9700\u8981\u6839\u636e\u7528\u6237\u4f20\u5165\u7684\u6587\u4ef6\u8def\u5f84\u5217\u8868&#xff0c;\u6253\u5f00\u6240\u6709\u6587\u4ef6\u8fdb\u884c\u5904\u7406&#xff1a;<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">process_files<\/span><span class=\"token punctuation\">(<\/span>paths<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    files <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><br \/>\n    <span class=\"token keyword\">try<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> path <span class=\"token keyword\">in<\/span> paths<span class=\"token punctuation\">:<\/span><br \/>\n            files<span class=\"token punctuation\">.<\/span>append<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span>path<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token comment\"># \u5904\u7406 files<\/span><br \/>\n    <span class=\"token keyword\">finally<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> f <span class=\"token keyword\">in<\/span> files<span class=\"token punctuation\">:<\/span><br \/>\n            f<span class=\"token punctuation\">.<\/span>close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u867d\u7136\u80fd\u5de5\u4f5c&#xff0c;\u4f46\u5982\u679c open \u8fc7\u7a0b\u4e2d\u67d0\u4e2a\u6587\u4ef6\u6253\u5f00\u5931\u8d25&#xff0c;\u4f60\u9700\u8981\u4fdd\u8bc1\u4e4b\u524d\u6253\u5f00\u7684\u6587\u4ef6\u88ab\u6b63\u786e\u5173\u95ed\u3002\u6b64\u65f6\u624b\u52a8\u7ba1\u7406\u4f1a\u53d8\u5f97\u8106\u5f31&#xff0c;\u800c\u4e14\u5728\u5f02\u5e38\u53d1\u751f\u65f6&#xff0c;finally \u53ef\u80fd\u63a9\u76d6\u539f\u59cb\u5f02\u5e38\u3002<\/p>\n<h4>\u573a\u666f 3&#xff1a;\u9700\u8981\u6df7\u5408\u591a\u79cd\u8d44\u6e90&#xff08;\u6587\u4ef6\u3001\u9501\u3001\u8fde\u63a5&#xff09;\u5e76\u786e\u4fdd\u5f02\u5e38\u65f6\u5168\u90e8\u91ca\u653e<\/h4>\n<p>\u4f60\u6709\u4e00\u4e2a\u4efb\u52a1&#xff0c;\u9700\u8981\u83b7\u53d6\u6570\u636e\u5e93\u8fde\u63a5\u3001\u6587\u4ef6\u9501&#xff0c;\u5e76\u6253\u5f00\u4e34\u65f6\u6587\u4ef6\u3002\u4efb\u4f55\u4e00\u4e2a\u6b65\u9aa4\u5931\u8d25&#xff0c;\u90fd\u5fc5\u987b\u91ca\u653e\u4e4b\u524d\u5df2\u83b7\u53d6\u7684\u8d44\u6e90&#xff0c;\u5426\u5219\u5c31\u4f1a\u6b7b\u9501\u3001\u6cc4\u6f0f\u3002<\/p>\n<h4>\u573a\u666f 4&#xff1a;\u5728\u9000\u51fa\u65f6\u9700\u8981\u6309\u7279\u5b9a\u987a\u5e8f\u6267\u884c\u6e05\u7406<\/h4>\n<p>\u4f8b\u5982&#xff0c;\u4f60\u9700\u8981\u5148\u4fdd\u5b58\u5e76\u5173\u95ed\u6587\u4ef6&#xff0c;\u7136\u540e\u91ca\u653e\u9501&#xff0c;\u6700\u540e\u65ad\u5f00\u6570\u636e\u5e93\u8fde\u63a5\u3002\u5982\u679c\u987a\u5e8f\u98a0\u5012&#xff0c;\u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u635f\u574f\u3002<\/p>\n<h3>\u4e8c\u3001\u5e95\u5c42\u539f\u7406&#xff1a;ExitStack \u5982\u4f55\u6210\u4e3a\u201c\u8d44\u6e90\u7ba1\u5bb6\u201d<\/h3>\n<h4>1. \u57fa\u672c\u7528\u6cd5<\/h4>\n<p><span class=\"token keyword\">from<\/span> contextlib <span class=\"token keyword\">import<\/span> ExitStack<\/p>\n<p><span class=\"token keyword\">with<\/span> ExitStack<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> stack<span class=\"token punctuation\">:<\/span><br \/>\n    file1 <span class=\"token operator\">&#061;<\/span> stack<span class=\"token punctuation\">.<\/span>enter_context<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;file1.txt&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    file2 <span class=\"token operator\">&#061;<\/span> stack<span class=\"token punctuation\">.<\/span>enter_context<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;file2.txt&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token comment\"># \u4f7f\u7528 file1, file2<\/span><br \/>\n<span class=\"token comment\"># \u79bb\u5f00 with \u65f6&#xff0c;file2 \u5148\u9000\u51fa&#xff0c;file1 \u540e\u9000\u51fa&#xff08;LIFO \u987a\u5e8f&#xff09;<\/span><\/p>\n<p>enter_context() \u65b9\u6cd5\u4f1a\u5c06\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u6ce8\u518c\u5230\u6808\u4e2d\u3002\u5f53 with ExitStack() \u5757\u7ed3\u675f\u65f6&#xff0c;\u6808\u4f1a\u6309\u7167**\u540e\u8fdb\u5148\u51fa&#xff08;LIFO&#xff09;**\u7684\u987a\u5e8f\u8c03\u7528\u6bcf\u4e2a\u5df2\u6ce8\u518c\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7684 __exit__ \u65b9\u6cd5\u3002\u8fd9\u4fdd\u8bc1\u4e86\u8d44\u6e90\u91ca\u653e\u7684\u6b63\u786e\u987a\u5e8f\u3002<\/p>\n<h4>2. \u6ce8\u518c\u6e05\u7406\u56de\u8c03<\/h4>\n<p>\u9664\u4e86\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&#xff0c;\u4f60\u8fd8\u53ef\u4ee5\u6ce8\u518c\u4efb\u610f\u51fd\u6570&#xff1a;<\/p>\n<p>stack<span class=\"token punctuation\">.<\/span>callback<span class=\"token punctuation\">(<\/span>func<span class=\"token punctuation\">,<\/span> arg1<span class=\"token punctuation\">,<\/span> arg2<span class=\"token punctuation\">)<\/span><\/p>\n<p>\u5728\u9000\u51fa\u65f6&#xff0c;\u8fd9\u4e9b\u56de\u8c03\u4e5f\u4f1a\u6309 LIFO \u987a\u5e8f\u8c03\u7528\u3002\u8fd9\u5bf9\u4e8e\u975e with \u517c\u5bb9\u7684\u8d44\u6e90&#xff08;\u5982\u9700\u8981\u624b\u52a8 close() \u7684\u5bf9\u8c61&#xff09;\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h4>3. \u5f02\u5e38\u5904\u7406\u4e0e\u56de\u6eda<\/h4>\n<p>ExitStack \u5728\u9000\u51fa\u65f6\u4f1a\u9010\u4e2a\u8c03\u7528\u6e05\u7406\u51fd\u6570\u3002\u5982\u679c\u67d0\u4e2a\u6e05\u7406\u51fd\u6570\u629b\u51fa\u4e86\u5f02\u5e38&#xff0c;\u5b83\u4f1a\u6682\u65f6\u8bb0\u5f55\u8be5\u5f02\u5e38&#xff0c;\u5e76\u7ee7\u7eed\u6267\u884c\u5269\u4f59\u7684\u6e05\u7406\u64cd\u4f5c&#xff0c;\u6700\u540e\u5c06\u6240\u6709\u6e05\u7406\u8fc7\u7a0b\u4e2d\u53d1\u751f\u7684\u5f02\u5e38\u6c47\u603b\u6210\u4e00\u6761\u9519\u8bef\u94fe&#xff08;\u901a\u8fc7 __context__&#xff09;\u5411\u4e0a\u629b\u51fa\u3002\u8fd9\u4e0e\u7eaf\u624b\u52a8\u7684 try\/finally \u4e0d\u540c&#xff0c;\u540e\u8005\u901a\u5e38\u4f1a\u5728\u7b2c\u4e00\u4e2a finally \u5f02\u5e38\u5904\u505c\u6b62&#xff0c;\u5bfc\u81f4\u540e\u7eed\u8d44\u6e90\u65e0\u6cd5\u91ca\u653e\u3002ExitStack \u7684\u8fd9\u4e00\u7279\u6027\u6781\u5927\u5730\u63d0\u5347\u4e86\u8d44\u6e90\u6e05\u7406\u7684\u5065\u58ee\u6027\u3002<\/p>\n<h4>4. \u52a8\u6001\u6570\u91cf\u8d44\u6e90\u7684\u81ea\u7136\u652f\u6301<\/h4>\n<p>\u7531\u4e8e enter_context \u53ef\u4ee5\u5728\u5faa\u73af\u4e2d\u968f\u65f6\u8c03\u7528&#xff0c;\u4f60\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u52a8\u6001\u5217\u8868&#xff1a;<\/p>\n<p><span class=\"token keyword\">with<\/span> ExitStack<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> stack<span class=\"token punctuation\">:<\/span><br \/>\n    files <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span>stack<span class=\"token punctuation\">.<\/span>enter_context<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span>path<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">for<\/span> path <span class=\"token keyword\">in<\/span> paths<span class=\"token punctuation\">]<\/span><br \/>\n    <span class=\"token comment\"># \u5904\u7406 files<\/span><\/p>\n<h4>5. \u4e0e with \u8bed\u53e5\u7684\u5d4c\u5957\u5bf9\u6bd4<\/h4>\n<p>ExitStack \u4e0d\u4ec5\u51cf\u5c11\u4e86\u7f29\u8fdb&#xff0c;\u8fd8\u8ba9\u4f60\u80fd\u7f16\u5199\u66f4\u53ef\u7ef4\u62a4\u7684\u8d44\u6e90\u7ba1\u7406\u4ee3\u7801\u3002\u7279\u522b\u662f\u5728\u6709\u591a\u4e2a\u6761\u4ef6\u5206\u652f\u65f6&#xff0c;\u4f60\u53ef\u4ee5\u53ea\u5728\u67d0\u4e2a\u5206\u652f\u4e0b\u624d\u6ce8\u518c\u67d0\u4e2a\u8d44\u6e90\u3002<\/p>\n<h3>\u4e09\u3001\u5e38\u89c1\u9677\u9631\u4e0e\u707e\u96be\u6027\u540e\u679c<\/h3>\n<h4>\u9677\u9631 1&#xff1a;\u5728 ExitStack \u5757\u5916\u4f7f\u7528\u5df2\u6ce8\u518c\u7684\u8d44\u6e90<\/h4>\n<p>stack <span class=\"token operator\">&#061;<\/span> ExitStack<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\nf <span class=\"token operator\">&#061;<\/span> stack<span class=\"token punctuation\">.<\/span>enter_context<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;file.txt&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\nstack<span class=\"token punctuation\">.<\/span>close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>   <span class=\"token comment\"># \u624b\u52a8\u5173\u95ed\u6808<\/span><br \/>\n<span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>f<span class=\"token punctuation\">.<\/span>read<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token comment\"># \u9519\u8bef&#xff01;\u6587\u4ef6\u5df2\u5173\u95ed<\/span><\/p>\n<p>\u4e00\u65e6\u6808\u5173\u95ed&#xff0c;\u6240\u6709\u6ce8\u518c\u7684\u8d44\u6e90\u90fd\u4f1a\u88ab\u91ca\u653e\u3002\u5982\u679c\u4f60\u5728\u6808\u5173\u95ed\u540e\u4ecd\u7136\u4f7f\u7528\u8fd9\u4e9b\u8d44\u6e90&#xff0c;\u5c31\u4f1a\u5f15\u53d1 ValueError: I\/O operation on closed file\u3002\u52a1\u5fc5\u4fdd\u8bc1\u8d44\u6e90\u53ea\u5728 with \u5757\u5185\u4f7f\u7528\u3002<\/p>\n<h4>\u9677\u9631 2&#xff1a;\u6e05\u7406\u56de\u8c03\u4e2d\u629b\u51fa\u7684\u5f02\u5e38\u63a9\u76d6\u4e86\u539f\u59cb\u4e1a\u52a1\u5f02\u5e38<\/h4>\n<p><span class=\"token keyword\">with<\/span> ExitStack<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> stack<span class=\"token punctuation\">:<\/span><br \/>\n    stack<span class=\"token punctuation\">.<\/span>callback<span class=\"token punctuation\">(<\/span>cleanup_that_raises<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">raise<\/span> ValueError<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;\u4e1a\u52a1\u5f02\u5e38&#034;<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<p>\u5982\u679c cleanup_that_raises \u5728\u9000\u51fa\u65f6\u629b\u51fa\u4e86 RuntimeError&#xff0c;\u90a3\u4e48\u6700\u7ec8\u7684\u5f02\u5e38\u94fe\u4f1a\u540c\u65f6\u5305\u542b\u539f\u59cb ValueError \u548c\u65b0\u7684 RuntimeError\u3002\u5982\u679c\u4e0d\u7406\u89e3\u5f02\u5e38\u94fe&#xff0c;\u4f60\u53ef\u80fd\u4f1a\u4ee5\u4e3a\u4e1a\u52a1\u5f02\u5e38\u88ab\u541e\u6389\u4e86\u3002\u5b9e\u9645\u4e0a&#xff0c;Python 3 \u4f1a\u663e\u793a\u5b8c\u6574\u7684\u5f02\u5e38\u94fe&#xff0c;\u4f46\u5982\u679c\u4f60\u53ea\u6355\u83b7\u4e86 Exception \u5e76\u6253\u5370\u4e86\u6700\u5916\u5c42&#xff0c;\u53ef\u80fd\u4f1a\u770b\u5230\u7684\u662f\u6e05\u7406\u5f02\u5e38\u3002<\/p>\n<h4>\u9677\u9631 3&#xff1a;\u9519\u8bef\u5730\u8ba4\u4e3a ExitStack \u4f1a\u541e\u6389\u6240\u6709\u5f02\u5e38<\/h4>\n<p>ExitStack \u4e0d\u4f1a\u541e\u6389 BaseException&#xff08;\u5982 KeyboardInterrupt&#xff09;&#xff0c;\u5b83\u4f1a\u6b63\u5e38\u4f20\u64ad\u3002\u4f46\u5b83\u4f1a\u6267\u884c\u6e05\u7406&#xff0c;\u8fd9\u662f\u7b26\u5408\u9884\u671f\u7684\u3002\u5982\u679c\u4f60\u5728\u6e05\u7406\u51fd\u6570\u4e2d\u6355\u83b7\u4e86 BaseException&#xff0c;\u5219\u53ef\u80fd\u963b\u6b62\u7a0b\u5e8f\u9000\u51fa&#xff0c;\u8fd9\u662f\u5371\u9669\u7684\u505a\u6cd5\u3002<\/p>\n<h4>\u9677\u9631 4&#xff1a;\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u5171\u4eab ExitStack<\/h4>\n<p>ExitStack \u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002\u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u6ce8\u518c\u548c\u9000\u51fa&#xff0c;\u4f1a\u5bfc\u81f4\u8d44\u6e90\u7ba1\u7406\u6df7\u4e71\u3002\u6bcf\u4e2a\u7ebf\u7a0b\u5e94\u8be5\u4f7f\u7528\u81ea\u5df1\u7684 ExitStack\u3002<\/p>\n<h4>\u9677\u9631 5&#xff1a;\u6ce8\u518c\u987a\u5e8f\u4e0e\u4f9d\u8d56\u5173\u7cfb\u5904\u7406\u4e0d\u5f53<\/h4>\n<p>\u5982\u679c\u8d44\u6e90 A \u4f9d\u8d56\u8d44\u6e90 B&#xff0c;\u90a3\u4e48\u5728\u9000\u51fa\u65f6 A \u5e94\u8be5\u5728 B \u4e4b\u524d\u91ca\u653e\u3002\u7531\u4e8e ExitStack \u4f7f\u7528 LIFO&#xff0c;\u8fd9\u610f\u5473\u7740 B \u5e94\u8be5\u5148\u6ce8\u518c&#xff0c;A \u540e\u6ce8\u518c\u3002\u5982\u679c\u4f60\u5f04\u53cd\u4e86&#xff0c;\u53ef\u80fd\u5728\u91ca\u653e A \u65f6 B \u5df2\u7ecf\u4e0d\u53ef\u7528&#xff0c;\u5bfc\u81f4\u5f02\u5e38\u3002<\/p>\n<p><span class=\"token keyword\">with<\/span> ExitStack<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> stack<span class=\"token punctuation\">:<\/span><br \/>\n    b <span class=\"token operator\">&#061;<\/span> stack<span class=\"token punctuation\">.<\/span>enter_context<span class=\"token punctuation\">(<\/span>create_b<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    a <span class=\"token operator\">&#061;<\/span> stack<span class=\"token punctuation\">.<\/span>enter_context<span class=\"token punctuation\">(<\/span>create_a<span class=\"token punctuation\">(<\/span>b<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># a \u4f9d\u8d56 b<\/span><br \/>\n    <span class=\"token comment\"># \u9000\u51fa\u987a\u5e8f&#xff1a;a \u5148\u9000\u51fa&#xff0c;b \u540e\u9000\u51fa&#xff0c;\u8fd9\u662f\u6b63\u786e\u7684<\/span><\/p>\n<h4>\u9677\u9631 6&#xff1a;\u4f7f\u7528 enter_context \u4f46\u5fd8\u8bb0\u5c06\u8fd4\u56de\u503c\u8d4b\u7ed9\u53d8\u91cf<\/h4>\n<p><span class=\"token keyword\">with<\/span> ExitStack<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> stack<span class=\"token punctuation\">:<\/span><br \/>\n    stack<span class=\"token punctuation\">.<\/span>enter_context<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;file.txt&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token comment\"># \u8fd4\u56de\u503c\u88ab\u4e22\u5f03&#xff0c;\u65e0\u6cd5\u4f7f\u7528\u6587\u4ef6<\/span><\/p>\n<p>\u8fd9\u4f1a\u5bfc\u81f4\u6587\u4ef6\u6253\u5f00\u540e\u65e0\u6cd5\u8bbf\u95ee&#xff0c;\u4e14\u53ef\u80fd\u5728\u9000\u51fa\u65f6\u6b63\u5e38\u5173\u95ed&#xff0c;\u4f46\u4f60\u767d\u505a\u4e86\u5de5\u4f5c\u3002\u52a1\u5fc5\u63a5\u6536\u8fd4\u56de\u503c\u3002<\/p>\n<h3>\u56db\u3001\u6b63\u786e\u4f7f\u7528 ExitStack \u7684\u9ec4\u91d1\u6a21\u5f0f<\/h3>\n<h4>1. \u52a8\u6001\u6587\u4ef6\u5904\u7406<\/h4>\n<p><span class=\"token keyword\">from<\/span> contextlib <span class=\"token keyword\">import<\/span> ExitStack<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">read_many<\/span><span class=\"token punctuation\">(<\/span>paths<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">with<\/span> ExitStack<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> stack<span class=\"token punctuation\">:<\/span><br \/>\n        files <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span>stack<span class=\"token punctuation\">.<\/span>enter_context<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span>p<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">for<\/span> p <span class=\"token keyword\">in<\/span> paths<span class=\"token punctuation\">]<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> f <span class=\"token keyword\">in<\/span> files<span class=\"token punctuation\">:<\/span><br \/>\n            <span class=\"token keyword\">print<\/span><span class=\"token punctuation\">(<\/span>f<span class=\"token punctuation\">.<\/span>read<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h4>2. \u6df7\u5408\u8d44\u6e90\u7ba1\u7406<\/h4>\n<p><span class=\"token keyword\">with<\/span> ExitStack<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> stack<span class=\"token punctuation\">:<\/span><br \/>\n    conn <span class=\"token operator\">&#061;<\/span> stack<span class=\"token punctuation\">.<\/span>enter_context<span class=\"token punctuation\">(<\/span>db<span class=\"token punctuation\">.<\/span>connect<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    lock <span class=\"token operator\">&#061;<\/span> stack<span class=\"token punctuation\">.<\/span>enter_context<span class=\"token punctuation\">(<\/span>threading<span class=\"token punctuation\">.<\/span>Lock<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token builtin\">file<\/span> <span class=\"token operator\">&#061;<\/span> stack<span class=\"token punctuation\">.<\/span>enter_context<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;data.txt&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token comment\"># \u9000\u51fa\u65f6 file, lock, conn \u4f9d\u6b21\u5173\u95ed<\/span><\/p>\n<h4>3. \u6e05\u7406\u56de\u8c03<\/h4>\n<p><span class=\"token keyword\">with<\/span> ExitStack<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> stack<span class=\"token punctuation\">:<\/span><br \/>\n    temp_dir <span class=\"token operator\">&#061;<\/span> create_temp_dir<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    stack<span class=\"token punctuation\">.<\/span>callback<span class=\"token punctuation\">(<\/span>shutil<span class=\"token punctuation\">.<\/span>rmtree<span class=\"token punctuation\">,<\/span> temp_dir<span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token comment\"># \u4f7f\u7528 temp_dir<\/span><br \/>\n<span class=\"token comment\"># \u81ea\u52a8\u9012\u5f52\u5220\u9664\u4e34\u65f6\u76ee\u5f55<\/span><\/p>\n<h4>4. \u5728\u51fd\u6570\u4e2d\u8fd4\u56de\u8d44\u6e90&#xff0c;\u4ea4\u7ed9\u8c03\u7528\u8005\u7ba1\u7406<\/h4>\n<p>\u6709\u65f6\u4f60\u60f3\u628a\u8d44\u6e90\u7ba1\u7406\u8d23\u4efb\u4ea4\u51fa\u53bb&#xff0c;\u53ef\u4ee5\u8fd9\u6837&#xff1a;<\/p>\n<p><span class=\"token keyword\">def<\/span> <span class=\"token function\">open_multiple<\/span><span class=\"token punctuation\">(<\/span>paths<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    stack <span class=\"token operator\">&#061;<\/span> ExitStack<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    <span class=\"token keyword\">try<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        files <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">[<\/span>stack<span class=\"token punctuation\">.<\/span>enter_context<span class=\"token punctuation\">(<\/span><span class=\"token builtin\">open<\/span><span class=\"token punctuation\">(<\/span>p<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">for<\/span> p <span class=\"token keyword\">in<\/span> paths<span class=\"token punctuation\">]<\/span><br \/>\n    <span class=\"token keyword\">except<\/span><span class=\"token punctuation\">:<\/span><br \/>\n        stack<span class=\"token punctuation\">.<\/span>close<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>   <span class=\"token comment\"># \u51fa\u9519\u65f6\u7acb\u5373\u5173\u95ed\u5df2\u6253\u5f00\u7684\u8d44\u6e90<\/span><br \/>\n        <span class=\"token keyword\">raise<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> files<span class=\"token punctuation\">,<\/span> stack  <span class=\"token comment\"># \u8fd4\u56de\u6808&#xff0c;\u8c03\u7528\u8005\u8d1f\u8d23\u5173\u95ed<\/span><\/p>\n<p>\u8c03\u7528\u8005&#xff1a;<\/p>\n<p>files<span class=\"token punctuation\">,<\/span> stack <span class=\"token operator\">&#061;<\/span> open_multiple<span class=\"token punctuation\">(<\/span>paths<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">with<\/span> stack<span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token comment\"># \u4f7f\u7528 files<\/span><\/p>\n<h4>5. \u4e0e contextlib.ExitStack \u5904\u7406\u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&#xff08;Python 3.11&#043; \u652f\u6301 async with&#xff09;<\/h4>\n<p>Python 3.11 \u5f15\u5165\u4e86 AsyncExitStack&#xff0c;\u7528\u4e8e\u7ba1\u7406\u5f02\u6b65\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&#xff1a;<\/p>\n<p><span class=\"token keyword\">from<\/span> contextlib <span class=\"token keyword\">import<\/span> AsyncExitStack<\/p>\n<p><span class=\"token keyword\">async<\/span> <span class=\"token keyword\">def<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">:<\/span><br \/>\n    <span class=\"token keyword\">async<\/span> <span class=\"token keyword\">with<\/span> AsyncExitStack<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">as<\/span> stack<span class=\"token punctuation\">:<\/span><br \/>\n        client <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">await<\/span> stack<span class=\"token punctuation\">.<\/span>enter_async_context<span class=\"token punctuation\">(<\/span>async_client<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><br \/>\n        <span class=\"token keyword\">await<\/span> client<span class=\"token punctuation\">.<\/span>fetch<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><\/p>\n<h3>\u4e94\u3001\u8c03\u8bd5\u4e0e\u9884\u9632\u5efa\u8bae<\/h3>\n<li>\u5728\u6e05\u7406\u51fd\u6570\u4e2d\u6dfb\u52a0\u65e5\u5fd7&#xff1a;\u786e\u8ba4\u6e05\u7406\u987a\u5e8f\u548c\u662f\u5426\u88ab\u8c03\u7528\u3002<\/li>\n<li>\u6d4b\u8bd5\u5f02\u5e38\u8def\u5f84&#xff1a;\u6a21\u62df\u6e05\u7406\u51fd\u6570\u629b\u51fa\u5f02\u5e38&#xff0c;\u68c0\u67e5\u5f02\u5e38\u94fe\u662f\u5426\u5b8c\u6574\u3002<\/li>\n<li>\u4f7f\u7528 try\/except \u5305\u88f9 ExitStack \u5757&#xff0c;\u8bb0\u5f55 __cause__ \u4fe1\u606f\u3002<\/li>\n<li>\u907f\u514d\u5728 ExitStack \u4e2d\u6ce8\u518c\u8fc7\u591a\u7684\u56de\u8c03&#xff0c;\u4fdd\u6301\u903b\u8f91\u6e05\u6670\u3002<\/li>\n<li>\u4ee3\u7801\u5ba1\u67e5\u65f6&#xff0c;\u68c0\u67e5\u8d44\u6e90\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb&#xff0c;\u786e\u4fdd\u6ce8\u518c\u987a\u5e8f\u6b63\u786e\u3002<\/li>\n<li>\u5bf9\u4e8e\u590d\u6742\u7684\u8d44\u6e90\u751f\u547d\u5468\u671f&#xff0c;\u53ef\u4ee5\u5c01\u88c5\u6210\u81ea\u5b9a\u4e49\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668&#xff0c;\u800c\u4e0d\u662f\u628a\u6240\u6709\u903b\u8f91\u90fd\u5806\u5728 ExitStack \u91cc\u3002<\/li>\n<h3>\u516d\u3001\u6700\u4f73\u5b9e\u8df5\u603b\u7ed3<\/h3>\n<ul>\n<li>\u7528 ExitStack \u7ba1\u7406\u52a8\u6001\u6570\u91cf\u6216\u8fc7\u591a\u7684\u8d44\u6e90&#xff0c;\u907f\u514d\u5d4c\u5957\u548c\u624b\u52a8 try\/finally\u3002<\/li>\n<li>\u7262\u8bb0\u9000\u51fa\u987a\u5e8f\u662f LIFO&#xff0c;\u786e\u4fdd\u4f9d\u8d56\u5173\u7cfb\u5408\u7406\u3002<\/li>\n<li>\u6ce8\u518c\u56de\u8c03\u65f6&#xff0c;\u4f7f\u7528 stack.callback \u5904\u7406\u975e\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u7684\u6e05\u7406\u3002<\/li>\n<li>ExitStack \u4f1a\u81ea\u52a8\u6c47\u603b\u6e05\u7406\u5f02\u5e38&#xff0c;\u4f46\u4f60\u9700\u8981\u7406\u89e3\u5f02\u5e38\u94fe\u3002<\/li>\n<li>\u4e0d\u8981\u5728\u591a\u7ebf\u7a0b\u95f4\u5171\u4eab\u540c\u4e00\u4e2a ExitStack\u3002<\/li>\n<li>\u5982\u679c\u8d44\u6e90\u9700\u8981\u5728 with \u5757\u5916\u4f7f\u7528&#xff0c;\u5e94\u624b\u52a8\u7ba1\u7406 ExitStack \u7684\u751f\u547d\u5468\u671f&#xff0c;\u6216\u4f7f\u7528\u5176\u4ed6\u6a21\u5f0f\u3002<\/li>\n<li>\u5728 Python 3.11&#043; \u4e2d&#xff0c;\u4f7f\u7528 AsyncExitStack \u7ba1\u7406\u5f02\u6b65\u8d44\u6e90\u3002<\/li>\n<li>\u4e3a\u6e05\u7406\u64cd\u4f5c\u7f16\u5199\u5355\u5143\u6d4b\u8bd5&#xff0c;\u7279\u522b\u662f\u5f02\u5e38\u53d1\u751f\u65f6\u7684\u6e05\u7406\u8def\u5f84\u3002<\/li>\n<\/ul>\n<h3>\u4e03\u3001\u7ed3\u8bed<\/h3>\n<p>contextlib.ExitStack \u5c31\u50cf\u4e00\u4f4d\u7ec6\u5fc3\u7684\u7ba1\u5bb6&#xff0c;\u5b83\u5c06\u4f60\u6240\u6709\u9700\u8981\u7167\u987e\u7684\u8d44\u6e90\u4e00\u4e00\u767b\u8bb0\u5728\u518c&#xff0c;\u5e76\u5728\u79bb\u5f00\u65f6\u6309\u6b63\u786e\u7684\u987a\u5e8f\u3001\u53ef\u9760\u5730\u9010\u4e00\u9001\u522b\u3002\u5b83\u8ba9\u4f60\u4ece\u201c\u5957\u5a03\u5730\u72f1\u201d\u4e2d\u89e3\u8131\u51fa\u6765&#xff0c;\u628a\u66f4\u591a\u7cbe\u529b\u653e\u5728\u4e1a\u52a1\u903b\u8f91\u4e0a&#xff0c;\u800c\u4e0d\u662f\u8d44\u6e90\u7ba1\u7406\u7684\u7ec6\u679d\u672b\u8282\u3002\u4f46\u7ba1\u5bb6\u4e5f\u6709\u539f\u5219&#xff1a;\u4ed6\u6309\u7167\u540e\u8fdb\u5148\u51fa\u7684\u987a\u5e8f\u505a\u4e8b&#xff0c;\u5982\u679c\u4f60\u628a\u4f9d\u8d56\u5173\u7cfb\u641e\u53cd&#xff0c;\u4ed6\u4e5f\u4f1a\u4e00\u4e1d\u4e0d\u82df\u5730\u6267\u884c\u9519\u8bef\u7684\u987a\u5e8f\u3002\u638c\u63e1\u4ed6\u7684\u813e\u6027&#xff0c;\u4f60\u5c31\u80fd\u5728 Python \u7684\u8d44\u6e90\u4e16\u754c\u91cc\u5982\u9c7c\u5f97\u6c34&#xff0c;\u65e0\u8bba\u9762\u5bf9\u591a\u5c11\u6587\u4ef6\u548c\u9501&#xff0c;\u90fd\u80fd\u4f18\u96c5\u5730\u8f6c\u8eab\u79bb\u53bb&#xff0c;\u4e0d\u7559\u4e0b\u4e00\u4e1d\u6cc4\u6f0f\u7684\u75d5\u8ff9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f60\u7684\u591a\u4e2a with \u5d4c\u5957\u4e3a\u4f55\u50cf\u201c\u5957\u5a03\u5730\u72f1\u201d&#xff1f;\u2014\u2014Python contextlib.ExitStack \u7684\u591a\u4e0a\u4e0b\u6587\u7ba1\u7406\u827a\u672f\u4e0e\u81f4\u547d\u9677\u9631\u5728 Python \u4e2d&#xff0c;with \u8bed\u53e5\u662f\u8d44\u6e90\u7ba1\u7406\u7684\u9ec4\u91d1\u6807\u51c6\u3002\u4f46\u5f53\u9700\u8981\u540c\u65f6\u7ba1\u7406\u591a\u4e2a\u8d44\u6e90\u2014\u2014\u6bd4\u5982\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\u3001\u83b7\u53d6\u591a\u628a\u9501\u3001\u5efa\u7acb\u591a\u4e2a\u8fde\u63a5\u2014\u2014\u4f60\u53ef\u80fd\u4f1a\u9677\u5165\u4e00\u79cd\u201c\u5957\u5a03\u5730\u72f1\u201d&#xff1a;with \u4e00\u5c42\u5957\u4e00\u5c42&#xff0c;\u7f29\u8fdb\u8d8a\u6765\u8d8a\u6df1&#xff0c;\u4ee3\u7801\u53ef\u8bfb\u6027\u6025\u5267\u4e0b\u964d\u3002\u66f4\u7cdf\u7684\u662f&#xff0c;\u5982\u679c\u8fd9\u4e9b\u8d44\u6e90\u662f\u52a8\u6001\u6570\u91cf\u7684&amp;#xff0<\/p>\n","protected":false},"author":2,"featured_media":97162,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,81,87],"topic":[],"class_list":["post-97163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-java","tag-python","tag-87"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4f60\u7684\u591a\u4e2a with \u5d4c\u5957\u4e3a\u4f55\u50cf\u201c\u5957\u5a03\u5730\u72f1\u201d\uff1f\u2014\u2014Python contextlib.ExitStack \u7684\u591a\u4e0a\u4e0b\u6587\u7ba1\u7406\u827a\u672f\u4e0e\u81f4\u547d\u9677\u9631 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/97163.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4f60\u7684\u591a\u4e2a with \u5d4c\u5957\u4e3a\u4f55\u50cf\u201c\u5957\u5a03\u5730\u72f1\u201d\uff1f\u2014\u2014Python contextlib.ExitStack \u7684\u591a\u4e0a\u4e0b\u6587\u7ba1\u7406\u827a\u672f\u4e0e\u81f4\u547d\u9677\u9631 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4f60\u7684\u591a\u4e2a with \u5d4c\u5957\u4e3a\u4f55\u50cf\u201c\u5957\u5a03\u5730\u72f1\u201d&#xff1f;\u2014\u2014Python contextlib.ExitStack \u7684\u591a\u4e0a\u4e0b\u6587\u7ba1\u7406\u827a\u672f\u4e0e\u81f4\u547d\u9677\u9631\u5728 Python \u4e2d&#xff0c;with \u8bed\u53e5\u662f\u8d44\u6e90\u7ba1\u7406\u7684\u9ec4\u91d1\u6807\u51c6\u3002\u4f46\u5f53\u9700\u8981\u540c\u65f6\u7ba1\u7406\u591a\u4e2a\u8d44\u6e90\u2014\u2014\u6bd4\u5982\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\u3001\u83b7\u53d6\u591a\u628a\u9501\u3001\u5efa\u7acb\u591a\u4e2a\u8fde\u63a5\u2014\u2014\u4f60\u53ef\u80fd\u4f1a\u9677\u5165\u4e00\u79cd\u201c\u5957\u5a03\u5730\u72f1\u201d&#xff1a;with \u4e00\u5c42\u5957\u4e00\u5c42&#xff0c;\u7f29\u8fdb\u8d8a\u6765\u8d8a\u6df1&#xff0c;\u4ee3\u7801\u53ef\u8bfb\u6027\u6025\u5267\u4e0b\u964d\u3002\u66f4\u7cdf\u7684\u662f&#xff0c;\u5982\u679c\u8fd9\u4e9b\u8d44\u6e90\u662f\u52a8\u6001\u6570\u91cf\u7684&amp;#xff0\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/97163.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-28T20:57:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260828205701-6a91f61db0d33.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/97163.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/97163.html\",\"name\":\"\u4f60\u7684\u591a\u4e2a with \u5d4c\u5957\u4e3a\u4f55\u50cf\u201c\u5957\u5a03\u5730\u72f1\u201d\uff1f\u2014\u2014Python contextlib.ExitStack \u7684\u591a\u4e0a\u4e0b\u6587\u7ba1\u7406\u827a\u672f\u4e0e\u81f4\u547d\u9677\u9631 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-28T20:57:08+00:00\",\"dateModified\":\"2026-08-28T20:57:08+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/97163.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/97163.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/97163.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4f60\u7684\u591a\u4e2a with \u5d4c\u5957\u4e3a\u4f55\u50cf\u201c\u5957\u5a03\u5730\u72f1\u201d\uff1f\u2014\u2014Python contextlib.ExitStack \u7684\u591a\u4e0a\u4e0b\u6587\u7ba1\u7406\u827a\u672f\u4e0e\u81f4\u547d\u9677\u9631\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4f60\u7684\u591a\u4e2a with \u5d4c\u5957\u4e3a\u4f55\u50cf\u201c\u5957\u5a03\u5730\u72f1\u201d\uff1f\u2014\u2014Python contextlib.ExitStack \u7684\u591a\u4e0a\u4e0b\u6587\u7ba1\u7406\u827a\u672f\u4e0e\u81f4\u547d\u9677\u9631 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/97163.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4f60\u7684\u591a\u4e2a with \u5d4c\u5957\u4e3a\u4f55\u50cf\u201c\u5957\u5a03\u5730\u72f1\u201d\uff1f\u2014\u2014Python contextlib.ExitStack \u7684\u591a\u4e0a\u4e0b\u6587\u7ba1\u7406\u827a\u672f\u4e0e\u81f4\u547d\u9677\u9631 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4f60\u7684\u591a\u4e2a with \u5d4c\u5957\u4e3a\u4f55\u50cf\u201c\u5957\u5a03\u5730\u72f1\u201d&#xff1f;\u2014\u2014Python contextlib.ExitStack \u7684\u591a\u4e0a\u4e0b\u6587\u7ba1\u7406\u827a\u672f\u4e0e\u81f4\u547d\u9677\u9631\u5728 Python \u4e2d&#xff0c;with \u8bed\u53e5\u662f\u8d44\u6e90\u7ba1\u7406\u7684\u9ec4\u91d1\u6807\u51c6\u3002\u4f46\u5f53\u9700\u8981\u540c\u65f6\u7ba1\u7406\u591a\u4e2a\u8d44\u6e90\u2014\u2014\u6bd4\u5982\u6253\u5f00\u591a\u4e2a\u6587\u4ef6\u3001\u83b7\u53d6\u591a\u628a\u9501\u3001\u5efa\u7acb\u591a\u4e2a\u8fde\u63a5\u2014\u2014\u4f60\u53ef\u80fd\u4f1a\u9677\u5165\u4e00\u79cd\u201c\u5957\u5a03\u5730\u72f1\u201d&#xff1a;with \u4e00\u5c42\u5957\u4e00\u5c42&#xff0c;\u7f29\u8fdb\u8d8a\u6765\u8d8a\u6df1&#xff0c;\u4ee3\u7801\u53ef\u8bfb\u6027\u6025\u5267\u4e0b\u964d\u3002\u66f4\u7cdf\u7684\u662f&#xff0c;\u5982\u679c\u8fd9\u4e9b\u8d44\u6e90\u662f\u52a8\u6001\u6570\u91cf\u7684&amp;#xff0","og_url":"https:\/\/www.wsisp.com\/helps\/97163.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-28T20:57:08+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/08\/20260828205701-6a91f61db0d33.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/97163.html","url":"https:\/\/www.wsisp.com\/helps\/97163.html","name":"\u4f60\u7684\u591a\u4e2a with \u5d4c\u5957\u4e3a\u4f55\u50cf\u201c\u5957\u5a03\u5730\u72f1\u201d\uff1f\u2014\u2014Python contextlib.ExitStack \u7684\u591a\u4e0a\u4e0b\u6587\u7ba1\u7406\u827a\u672f\u4e0e\u81f4\u547d\u9677\u9631 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-28T20:57:08+00:00","dateModified":"2026-08-28T20:57:08+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/97163.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/97163.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/97163.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4f60\u7684\u591a\u4e2a with \u5d4c\u5957\u4e3a\u4f55\u50cf\u201c\u5957\u5a03\u5730\u72f1\u201d\uff1f\u2014\u2014Python contextlib.ExitStack \u7684\u591a\u4e0a\u4e0b\u6587\u7ba1\u7406\u827a\u672f\u4e0e\u81f4\u547d\u9677\u9631"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/97163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=97163"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/97163\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/97162"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=97163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=97163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=97163"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=97163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}