{"id":204993,"date":"2025-05-29T08:53:30","date_gmt":"2025-05-29T00:53:30","guid":{"rendered":"https:\/\/server.hk\/cnblog\/204993\/"},"modified":"2025-05-29T08:53:30","modified_gmt":"2025-05-29T00:53:30","slug":"python-with%e8%af%ad%e5%8f%a5%ef%bc%9areturn%e8%af%ad%e5%8f%a5%e5%90%8e%e6%96%87%e4%bb%b6%e4%bc%9a%e8%87%aa%e5%8a%a8%e5%85%b3%e9%97%ad%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/204993\/","title":{"rendered":"Python with\u8bed\u53e5\uff1areturn\u8bed\u53e5\u540e\u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\u5417\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Python with\u8bed\u53e5\uff1areturn\u8bed\u53e5\u540e\u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\u5417\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5206\u4eab\u300aPython with\u8bed\u53e5\uff1areturn\u8bed\u53e5\u540e\u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\u5417\uff1f\u300b\uff0c\u8986\u76d6\u4e86\u6587\u7ae0\u7684\u5e38\u89c1\u57fa\u7840\u77e5\u8bc6\uff0c\u5176\u5b9e\u4e00\u4e2a\u8bed\u8a00\u7684\u5168\u90e8\u77e5\u8bc6\u70b9\u4e00\u7bc7\u6587\u7ae0\u662f\u4e0d\u53ef\u80fd\u8bf4\u5b8c\u7684\uff0c\u4f46\u5e0c\u671b\u901a\u8fc7\u8fd9\u4e9b\u95ee\u9898\uff0c\u8ba9\u8bfb\u8005\u5bf9\u81ea\u5df1\u7684\u638c\u63e1\u7a0b\u5ea6\u6709\u4e00\u5b9a\u7684\u8ba4\u8bc6(B \u6570)\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u66f4\u597d\u7684\u638c\u63e1\u5b83\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241121\/1732155360673e97e045361.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>python \u4e2d\u7684 with \u8bed\u53e5\uff1af \u6587\u4ef6\u662f\u5426\u88ab\u5173\u95ed\uff1f<\/strong><\/p>\n<p>\u5728 python \u4e2d\uff0c\u4f7f\u7528 with \u8bed\u53e5\u53ef\u4ee5\u7b80\u5316\u5bf9\u6587\u4ef6\u7684\u64cd\u4f5c\u3002\u4e0d\u8fc7\u5f53\u9047\u5230 return \u8bed\u53e5\u65f6\uff0c\u53ef\u80fd\u4f1a\u8ba9\u4eba\u7591\u60d1\u6587\u4ef6\u662f\u5426\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u6765\u770b\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>def func():\n  with open(self.yaml_path,'w') as f:\n      info_save=yaml.load(f)\n      if xx:\n          return\n    \n  return<\/pre>\n<p>\u4ee3\u7801\u4e2d\u4f7f\u7528\u4e86 with \u8bed\u53e5\u6765\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6 f\uff0c\u7528\u4e8e\u5199\u5165\u64cd\u4f5c\u3002return \u8bed\u53e5\u5c06\u9000\u51fa\u51fd\u6570\u3002\u95ee\u9898\u5728\u4e8e\uff0c\u5f53\u7b2c\u4e00\u4e2a return \u8bed\u53e5\u88ab\u6267\u884c\u65f6\uff0c\u6587\u4ef6 f \u662f\u5426\u4f1a\u88ab\u5173\u95ed\u3002<\/p>\n<p>\u7b54\u6848\u662f\u80af\u5b9a\u7684\uff0c__\u4f1a\u5173\u95ed__\u3002<\/p>\n<p>\u5f53\u4f7f\u7528 with \u8bed\u53e5\u65f6\uff0c\u4f1a\u521b\u5efa\u4e00\u4e2a\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5bf9\u8c61\u3002\u8be5\u5bf9\u8c61\u5728\u8fdb\u5165\u4ee3\u7801\u5757\u65f6\u88ab\u521b\u5efa\uff0c\u5e76\u5728\u9000\u51fa\u4ee3\u7801\u5757\uff08\u65e0\u8bba\u662f\u6b63\u5e38\u9000\u51fa\u8fd8\u662f\u5f02\u5e38\u9000\u51fa\uff09\u65f6\u88ab\u9500\u6bc1\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u5bf9\u8c61\u8d1f\u8d23\u7ba1\u7406\u6587\u4ef6 f\uff0c\u786e\u4fdd\u5728\u5176\u9500\u6bc1\u65f6\u6587\u4ef6\u88ab\u5173\u95ed\u3002\u56e0\u6b64\uff0c\u5f53\u7b2c\u4e00\u4e2a return \u8bed\u53e5\u88ab\u6267\u884c\u65f6\uff0c\u6587\u4ef6 f \u4f1a\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<p>\u6709\u5173 with \u8bed\u53e5\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u8003 [python \u5b98\u65b9\u6587\u6863](https:\/\/docs.python.org\/3\/reference\/compound_stmts.html#with)\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPython with\u8bed\u53e5\uff1areturn\u8bed\u53e5\u540e\u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\u5417\uff1f\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python with\u8bed\u53e5\uff1are&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-204993","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204993","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=204993"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/204993\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=204993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=204993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=204993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}