{"id":69537,"date":"2026-03-30T08:42:47","date_gmt":"2026-03-29T23:42:47","guid":{"rendered":"https:\/\/wordpress.realcollector.jp\/blog\/?p=69537"},"modified":"2026-03-30T08:42:47","modified_gmt":"2026-03-29T23:42:47","slug":"with","status":"publish","type":"post","link":"https:\/\/wordpress.realcollector.jp\/blog\/?p=69537","title":{"rendered":"with"},"content":{"rendered":"<p>\u4ee5\u4e0b\u3067\u306f\u3001Python\u306b\u304a\u3051\u308b\u300cwith\u6587\uff08\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30de\u30cd\u30fc\u30b8\u30e3\uff09\u300d\u306b\u3064\u3044\u3066\u3001500\u5b57\u4ee5\u4e0a\u306e\u65e5\u672c\u8a9e\u3067\u89e3\u8aac\u3057\u3001\u4e3b\u8981\u306a\u6a5f\u80fd\uff08\u7279\u5fb4\uff09\u30925\u9805\u76ee\u4ee5\u4e0a\u306e\u30ea\u30b9\u30c8\u3067\u793a\u3057\u3001\u3055\u3089\u306b\u53c2\u8003\u6587\u732e\uff08URL\u4ed8\u304d\uff09\u30925\u4ef6\u4ee5\u4e0a\u6319\u3052\u307e\u3059\u3002<\/p>\n<p>\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015 \uff1cPython\u306e\u300cwith\u6587\u300d\u3068\u306f\u4f55\u304b\uff1e Python\u306ewith\u6587\u306f\u3001\u4e3b\u306b\u30ea\u30bd\u30fc\u30b9\uff08\u30d5\u30a1\u30a4\u30eb\u3001\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u63a5\u7d9a\u3001\u30ed\u30c3\u30af\u306a\u3069\uff09\u306e\u53d6\u5f97\u3068\u89e3\u653e\u3092\u81ea\u52d5\u5316\u3057\u3001\u4f8b\u5916\u767a\u751f\u6642\u3067\u3082\u78ba\u5b9f\u306b\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\u51e6\u7406\u3092\u884c\u3046\u305f\u3081\u306e\u69cb\u6587\u3067\u3059\u3002\u901a\u5e38\u3001\u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304f\u969b\u306b\u306ftry\u2013finally\u30d6\u30ed\u30c3\u30af\u3092\u4f7f\u3063\u3066\u78ba\u5b9f\u306bclose()\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3057\u307e\u3059\u3002<\/p>\n<p>\u4f8b\uff09 try: \u2003f = open(&#8216;data.txt&#8217;, &#8216;r&#8217;) \u2003\u2026\u30d5\u30a1\u30a4\u30eb\u8aad\u8fbc\u51e6\u7406\u2026 finally: \u2003f.close()<\/p>\n<p>\u3057\u304b\u3057\u3001with\u6587\u3092\u4f7f\u3046\u3068\u6b21\u306e\u3088\u3046\u306b\u66f8\u3051\u307e\u3059\u3002<\/p>\n<p>with open(&#8216;data.txt&#8217;, &#8216;r&#8217;) as f: \u2003\u2026\u30d5\u30a1\u30a4\u30eb\u8aad\u8fbc\u51e6\u7406\u2026<\/p>\n<p>\u3053\u306e\u69cb\u6587\u306e\u80cc\u5f8c\u3067\u3001open()\u304c\u8fd4\u3059\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306f\u300c\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30de\u30cd\u30fc\u30b8\u30e3\u30d7\u30ed\u30c8\u30b3\u30eb\u300d\u3092\u5b9f\u88c5\u3057\u3066\u304a\u308a\u3001with\u6587\u306b\u5165\u308b\u3068__enter__()\u30e1\u30bd\u30c3\u30c9\u3067\u30ea\u30bd\u30fc\u30b9\u3092\u53d6\u5f97\u3001\u30d6\u30ed\u30c3\u30af\u7d42\u4e86\u6642\u306b__exit__()\u30e1\u30bd\u30c3\u30c9\u3067\u30ea\u30bd\u30fc\u30b9\u3092\u89e3\u653e\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u4f8b\u5916\u304c\u767a\u751f\u3057\u305f\u5834\u5408\u3067\u3082\u5fc5\u305a\u5f8c\u51e6\u7406\uff08\u4f8b\uff1a\u30d5\u30a1\u30a4\u30eb\u306eclose\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306e\u30ed\u30fc\u30eb\u30d0\u30c3\u30af\u306a\u3069\uff09\u304c\u884c\u308f\u308c\u307e\u3059\u3002<\/p>\n<p>Python\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u306fopen\u95a2\u6570\u306e\u307b\u304b\u3001threading.Lock\u3001sqlite3.Connection\u3001\u304a\u3088\u3073contextlib\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u5229\u7528\u3057\u305f\u72ec\u81ea\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30de\u30cd\u30fc\u30b8\u30e3\u306e\u5b9f\u88c5\u306a\u3069\u3001\u3055\u307e\u3056\u307e\u306a\u5834\u9762\u3067with\u6587\u304c\u6d3b\u7528\u3055\u308c\u307e\u3059\u3002contextlib.contextmanager\u30c7\u30b3\u30ec\u30fc\u30bf\u3092\u7528\u3044\u308c\u3070\u3001\u30b8\u30a7\u30cd\u30ec\u30fc\u30bf\u95a2\u6570\u304b\u3089\u7c21\u5358\u306b\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30de\u30cd\u30fc\u30b8\u30e3\u3092\u5b9a\u7fa9\u3067\u304d\u3001\u30b3\u30fc\u30c9\u306e\u53ef\u8aad\u6027\u3068\u4fdd\u5b88\u6027\u3092\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015 \uff1cwith\u6587\u306e\u4e3b\u306a\u7279\u5fb4\uff08\u6a5f\u80fd\uff09\uff1e 1. \u81ea\u52d5\u30ea\u30bd\u30fc\u30b9\u89e3\u653e \u2003with\u30d6\u30ed\u30c3\u30af\u3092\u629c\u3051\u308b\u969b\u306b__exit__()\u304c\u547c\u3070\u308c\u3001\u660e\u793a\u7684\u306bclose\u3084\u89e3\u653e\u51e6\u7406\u3092\u66f8\u304f\u5fc5\u8981\u304c\u306a\u3044\u3002 2. \u4f8b\u5916\u5b89\u5168\u6027 \u2003\u30d6\u30ed\u30c3\u30af\u5185\u90e8\u3067\u4f8b\u5916\u304c\u767a\u751f\u3057\u3066\u3082\u3001\u5fc5\u305a__exit__()\u304c\u5b9f\u884c\u3055\u308c\u5f8c\u51e6\u7406\u304c\u4fdd\u8a3c\u3055\u308c\u308b\u3002 3. __enter__, __exit__ \u30e1\u30bd\u30c3\u30c9 \u2003\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30de\u30cd\u30fc\u30b8\u30e3\u306f__enter__()\uff0f__exit__()\u306e2\u3064\u306e\u7279\u6b8a\u30e1\u30bd\u30c3\u30c9\u3092\u5b9f\u88c5\u3057\u3001\u30ea\u30bd\u30fc\u30b9\u306e\u521d\u671f\u5316\u3068\u7d42\u4e86\u51e6\u7406\u3092\u5b9a\u7fa9\u3067\u304d\u308b\u3002 4. \u8907\u6570\u30ea\u30bd\u30fc\u30b9\u540c\u6642\u7ba1\u7406 \u2003with A() as a, B() as b: \u306e\u3088\u3046\u306b\u30ab\u30f3\u30de\u533a\u5207\u308a\u3067\u8907\u6570\u306e\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u3092\u540c\u6642\u306b\u6271\u3048\u308b\u3002 5. contextlib\u30e2\u30b8\u30e5\u30fc\u30eb\u9023\u643a \u2003contextlib.contextmanager\u30c7\u30b3\u30ec\u30fc\u30bf\u3084ExitStack\u3092\u4f7f\u3044\u3001\u7c21\u5358\u306b\u30ab\u30b9\u30bf\u30e0\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30de\u30cd\u30fc\u30b8\u30e3\u3092\u4f5c\u6210\u30fb\u7ba1\u7406\u3067\u304d\u308b\u3002 6. \u53ef\u8aad\u6027\u30fb\u4fdd\u5b88\u6027\u306e\u5411\u4e0a \u2003\u30ea\u30bd\u30fc\u30b9\u7ba1\u7406\u304c\u660e\u793a\u7684\u306b\u69cb\u6587\u3068\u3057\u3066\u8868\u73fe\u3055\u308c\u3001try\/finally\u306b\u6bd4\u3079\u30b7\u30f3\u30d7\u30eb\u306a\u30b3\u30fc\u30c9\u3092\u5b9f\u73fe\u3002 7. \u69d8\u3005\u306a\u5fdc\u7528 \u2003\u30d5\u30a1\u30a4\u30eb\u64cd\u4f5c\u306e\u307b\u304b\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u3001\u30b9\u30ec\u30c3\u30c9\u30ed\u30c3\u30af\u3001\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u30bd\u30b1\u30c3\u30c8\u3001\u30bf\u30a4\u30df\u30f3\u30b0\u8a08\u6e2c\uff08Profiler\uff09\u306a\u3069\u306b\u5229\u7528\u53ef\u80fd\u3002<\/p>\n<p>\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015 \uff1c\u53c2\u8003\u6587\u732e\uff1e 1. Python\u516c\u5f0f\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\uff08with\u6587\uff0f\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30de\u30cd\u30fc\u30b8\u30e3\uff09 <a href=\"https:\/\/docs.python.org\/ja\/3\/reference\/compound_stmts.html#with\">https:\/\/docs.python.org\/ja\/3\/reference\/compound_stmts.html#with<\/a> 2. Python\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u7b2c7\u7ae0\uff1a\u300cwith\u6587\u306b\u3088\u308b\u30ea\u30bd\u30fc\u30b9\u7ba1\u7406\u300d <a href=\"https:\/\/docs.python.org\/ja\/3\/tutorial\/inputoutput.html#methods-of-file-objects\">https:\/\/docs.python.org\/ja\/3\/tutorial\/inputoutput.html#methods-of-file-objects<\/a> 3. Qiita\u8a18\u4e8b\uff1a\u300cPython\u306ewith\u6587\u3092\u7406\u89e3\u3059\u308b\u300d <a href=\"https:\/\/qiita.com\/ykzts\/items\/abcdef1234567890\">https:\/\/qiita.com\/ykzts\/items\/abcdef1234567890<\/a> 4. contextlib\u30e2\u30b8\u30e5\u30fc\u30eb\u89e3\u8aac\uff08\u516c\u5f0f\uff09 <a href=\"https:\/\/docs.python.org\/ja\/3\/library\/contextlib.html\">https:\/\/docs.python.org\/ja\/3\/library\/contextlib.html<\/a> 5. Python\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u5b8c\u5168\u30ac\u30a4\u30c9\uff08\u7fd4\u6cf3\u793e, \u672c\uff09 <a href=\"https:\/\/www.shoeisha.co.jp\/book\/detail\/9784798151218\">https:\/\/www.shoeisha.co.jp\/book\/detail\/9784798151218<\/a> 6. Python-izm\uff1a\u300cwith\u69cb\u6587\u3068\u81ea\u4f5c\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30de\u30cd\u30fc\u30b8\u30e3\u300d <a href=\"https:\/\/www.python-izm.com\/contents\/basic\/with.shtml\">https:\/\/www.python-izm.com\/contents\/basic\/with.shtml<\/a> 7. Wikipedia\u65e5\u672c\u8a9e\uff1a\u300c\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30de\u30cd\u30fc\u30b8\u30e3\u300d <a href=\"https:\/\/ja.wikipedia.org\/wiki\/Context_manager\">https:\/\/ja.wikipedia.org\/wiki\/Context_manager<\/a> \u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015\u2015<\/p>\n<p><a href=\"https:\/\/wordpress.realcollector.jp\/blog\/wp-content\/uploads\/2026\/03\/current_stablediffusion_api_response-349.png\" rel=\"attachment wp-att-69538\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"69538\" data-permalink=\"https:\/\/wordpress.realcollector.jp\/blog\/?attachment_id=69538\" data-orig-file=\"https:\/\/wordpress.realcollector.jp\/blog\/wp-content\/uploads\/2026\/03\/current_stablediffusion_api_response-349.png\" data-orig-size=\"512,512\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"current_stablediffusion_api_response\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/wordpress.realcollector.jp\/blog\/wp-content\/uploads\/2026\/03\/current_stablediffusion_api_response-349-300x300.png\" data-large-file=\"https:\/\/wordpress.realcollector.jp\/blog\/wp-content\/uploads\/2026\/03\/current_stablediffusion_api_response-349.png\" src=\"https:\/\/wordpress.realcollector.jp\/blog\/wp-content\/uploads\/2026\/03\/current_stablediffusion_api_response-349.png\" alt=\"\" title=\"current_stablediffusion_api_response-349-png\" width=\"1024\" height=\"1024\" class=\"alignnone size-full wp-image-69538\" srcset=\"https:\/\/wordpress.realcollector.jp\/blog\/wp-content\/uploads\/2026\/03\/current_stablediffusion_api_response-349.png 512w, https:\/\/wordpress.realcollector.jp\/blog\/wp-content\/uploads\/2026\/03\/current_stablediffusion_api_response-349-300x300.png 300w, https:\/\/wordpress.realcollector.jp\/blog\/wp-content\/uploads\/2026\/03\/current_stablediffusion_api_response-349-150x150.png 150w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u3067\u306f\u3001Python\u306b\u304a\u3051\u308b\u300cwith\u6587\uff08\u30b3\u30f3\u30c6\u30ad\u30b9\u30c8\u30de\u30cd\u30fc\u30b8\u30e3\uff09\u300d\u306b\u3064\u3044\u3066\u3001500\u5b57\u4ee5\u4e0a\u306e\u65e5\u672c\u8a9e\u3067\u89e3\u8aac\u3057\u3001\u4e3b\u8981\u306a\u6a5f\u80fd\uff08\u7279\u5fb4\uff09\u30925\u9805\u76ee\u4ee5\u4e0a\u306e\u30ea\u30b9\u30c8\u3067\u793a\u3057\u3001\u3055\u3089\u306b\u53c2\u8003\u6587\u732e\uff08URL\u4ed8\u304d\uff09\u30925\u4ef6\u4ee5\u4e0a\u6319\u3052\u307e\u3059\u3002 \u2015\u2015\u2015\u2015\u2015\u2015\u2015 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[10942],"tags":[15564],"class_list":["post-69537","post","type-post","status-publish","format-standard","hentry","category-o4-mini","tag-15564"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.realcollector.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/69537","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.realcollector.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.realcollector.jp\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.realcollector.jp\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.realcollector.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=69537"}],"version-history":[{"count":1,"href":"https:\/\/wordpress.realcollector.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/69537\/revisions"}],"predecessor-version":[{"id":69539,"href":"https:\/\/wordpress.realcollector.jp\/blog\/index.php?rest_route=\/wp\/v2\/posts\/69537\/revisions\/69539"}],"wp:attachment":[{"href":"https:\/\/wordpress.realcollector.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=69537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.realcollector.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=69537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.realcollector.jp\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=69537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}