<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                with...as python 控制流語句,是簡化版的try except finally。 with expression [as variable]: with-block with 語句實質是上下文管理。 1、上下文管理協議。包含方法__enter__() 和 __exit__(),支持該協議對象要實現這兩個方法。 2、上下文管理器,定義執行with語句時要建立的運行時上下文,負責執行with語句塊上下文中的進入與退出操作。 3、進入上下文的時候執行__enter__方法,如果設置as var語句,var變量接受__enter__()方法返回值。 4、如果運行時發生了異常,就退出上下文管理器。調用管理器__exit__方法。 應用場景 1、文件操作。 2、進程線程之間互斥對象。 3、支持上下文其他對象 1. 方式1 #?簡單處理 file?=?open("1.txt") data?=?file.read() file.close() 2. 方式2 #?增加異常處理 try: ????f?=?open('1.txt') except: ????print?'fail?to?open' ????exit(-1) finally ????f.close() 3. 方式3 #?使用with with?open("/tmp/1.txt")?as?file: ????data?=?file.read() ???? 1.?方式1?對異常沒處理 2.?方式2?寫法冗長 3. with 簡潔 with 語句,調用__enter__() 方法并返回值, 這個值賦給as后面的變量 with 后面的代碼執行完成,調用__exit__()方法 class Sample: def __enter__(self): print "In __enter__()" return "Foo" def __exit__(self, type, value, trace): print "In __exit__()" def get_sample(): return Sample() with get_sample() as sample: print "sample:", sample with 可以處理異常 with as 表達式簡化了finally工作,保持代碼的優雅性。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看