{"id":4160,"date":"2023-02-08T13:42:14","date_gmt":"2023-02-08T04:42:14","guid":{"rendered":"https:\/\/417.run\/?p=4160"},"modified":"2023-02-08T13:42:14","modified_gmt":"2023-02-08T04:42:14","slug":"with","status":"publish","type":"post","link":"https:\/\/417.run\/pg\/python\/with\/","title":{"rendered":"\u3010Python\u3011with\u69cb\u6587\u306e\u4ed5\u7d44\u307f\u3001\u66f8\u304d\u65b9"},"content":{"rendered":"<pre><code class=\"language-python\">with open(path) as f:\n    s = f.read()<\/code><\/pre>\n<p>\u307f\u305f\u3044\u306a\u611f\u3058\u3067\u307f\u308b<code>with<\/code>\u3067\u3059\u304c\u3001<br \/>\n\u4ed6\u306e\u8a00\u8a9e\u306b\u306f\u306a\u3044\u306e\u3067\u3001\u306a\u3093\u3068\u306a\u304f\u30b9\u30eb\u30fc\u3057\u3066\u4f7f\u3063\u3066\u3044\u307e\u3057\u305f\u304c<\/p>\n<p>\u3059\u3054\u304f\u4fbf\u5229\u3067\u7c21\u5358\u306b\u4f7f\u3048\u308b\u306e\u3067\u3001\u304a\u3059\u3059\u3081\u3067\u3059\u3002<\/p>\n<p>\u6bce\u56de\u540c\u3058\u521d\u671f\u5316\u3001\u7d42\u4e86\u51e6\u7406\u3092\u884c\u3046\u3082\u306e\u3092\u7dba\u9e97\u306b\u5171\u901a\u5316\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code class=\"language-python\">class Hoge():\n\n    def __init__(self, arg):\n        &#039;&#039;&#039;\u5f15\u6570\u304c\u3042\u308b\u5834\u5408\u306finit\u3092\u5229\u7528\u3059\u308b&#039;&#039;&#039;\n        print(arg)\n        print(&#039;init \u2190entry\u3088\u308a\u5148&#039;)\n\n    def __enter__(self):\n        &#039;&#039;&#039;\u958b\u59cb\u51e6\u7406&#039;&#039;&#039;\n        print(&#039;entry&#039;)\n\n        # with\u306eas\u306b\u306a\u308b\u90e8\u5206\u304c\u3053\u3053\n        return self\n\n    def __exit__(self, exc_type, exc_val, exc_tb):\n        &#039;&#039;&#039;\u7d42\u4e86\u51e6\u7406&#039;&#039;&#039;\n        print(&#039;exit&#039;)\n        if exc_type is not None:\n            # \u30a8\u30af\u30bb\u30d7\u30b7\u30e7\u30f3\u304c\u8d77\u304d\u305f\u5834\u5408\u306f\u3001\u60c5\u5831\u304c\u5165\u308b\n            print(exc_type)\n            print(exc_val)\n            print(exc_tb)\n\n        # True\u3092\u8fd4\u3059\u3068\u30a8\u30af\u30bb\u30d7\u30b7\u30e7\u30f3\u3092raise\u3057\u306a\u3044\n        return True\n\n    def test():\n         print(&#039;\u304a\u306f\u3088\u3046\u50d5\u306e\u4e16\u754c&#039;)<\/code><\/pre>\n<p>\u30af\u30e9\u30b9\u306b<code>__entry__<\/code>\u3068<code>__exit__<\/code>\u3092\u5b9f\u88c5\u3059\u308c\u3070\u3001with\u3067\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\uff01<\/p>\n<pre><code class=\"language-python\">with Hoge(&#039;hello&#039;) as hoge:\n    hoge.test()<\/code><\/pre>\n<pre><code>hello\ninit \u2190entry\u3088\u308a\u5148\nentry\n\u304a\u306f\u3088\u3046\u50d5\u306e\u4e16\u754c\nexit<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>with open(path) as f: s = f.read() \u307f\u305f\u3044\u306a\u611f\u3058\u3067\u307f\u308bwith\u3067\u3059\u304c\u3001 \u4ed6\u306e\u8a00\u8a9e\u306b\u306f\u306a\u3044\u306e\u3067\u3001\u306a\u3093\u3068\u306a\u304f\u30b9\u30eb\u30fc\u3057\u3066\u4f7f\u3063\u3066\u3044\u307e\u3057\u305f\u304c \u3059\u3054\u304f\u4fbf\u5229\u3067\u7c21\u5358\u306b\u4f7f\u3048\u308b\u306e\u3067\u3001\u304a\u3059\u3059\u3081\u3067\u3059\u3002 \u6bce\u56de\u540c\u3058 &#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":{"0":"post-4160","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python","7":"nothumb"},"_links":{"self":[{"href":"https:\/\/417.run\/wp-json\/wp\/v2\/posts\/4160"}],"collection":[{"href":"https:\/\/417.run\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/417.run\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/comments?post=4160"}],"version-history":[{"count":3,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/posts\/4160\/revisions"}],"predecessor-version":[{"id":4163,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/posts\/4160\/revisions\/4163"}],"wp:attachment":[{"href":"https:\/\/417.run\/wp-json\/wp\/v2\/media?parent=4160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/categories?post=4160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/417.run\/wp-json\/wp\/v2\/tags?post=4160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}