<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                >[success] # 定義類具有可以使用with ~~~ 1.當我們使用一些 需要執行關閉操作的 通常在代碼的最后會執行.close方法,有的只需要使用with 就可以自動關閉 2.創建一個可以被with使用的類需要需要實現 __enter()__ 和 __exit__() 方法 3.執行順序會自動先執行__enter()__ 方法后在執行 __exit__() 關閉操作 ~~~ >[danger] ##### 代碼案例 ~~~ 1.編寫上下文管理器的主要原理是你的代碼會放到 with 語句塊中執行。當出現 with 語句的時候,對象的 __enter__() 方法被 觸發,它返回的值(如果有的話)會被賦值給 as 聲明的變量。然后,with 語句塊里面的代碼開始執行。最后,__exit__() 方法被 觸發進行清理工作。 2.__exit__() 方法的第三個參數包含了異常類型、異常值和追溯信息 3.iter 替代while True,第一個參數是一個回調函數 ~~~ ~~~ from socket import socket, AF_INET, SOCK_STREAM class LazyConnection: def __init__(self, address, family=AF_INET, type=SOCK_STREAM): self.address = address self.family = family self.type = type self.sock = None def __enter__(self): if self.sock is not None: raise RuntimeError('Already connected') self.sock = socket(self.family, self.type) self.sock.connect(self.address) return self.sock def __exit__(self, exc_ty, exc_val, tb): self.sock.close() # 關閉后清空對象 self.sock = None conn = LazyConnection(('www.python.org', 80)) with conn as s: # conn.__enter__() executes: connection open s.send(b'GET /index.html HTTP/1.0\r\n') s.send(b'Host: www.python.org\r\n') s.send(b'\r\n') # 替代while True resp = b''.join(iter(lambda :s.recv(2048), b'')) print(resp.decode('utf-8')) ~~~ >[danger] ##### with 嵌套with ~~~ 1.創建一個鏈接池 2.利用彈棧刪除連接池內容并且關閉 ~~~ ~~~ from socket import socket, AF_INET, SOCK_STREAM class LazyConnection: def __init__(self, address, family=AF_INET, type=SOCK_STREAM): self.address = address self.family = family self.type = type self.connections = [] def __enter__(self): sock = socket(self.family, self.type) sock.connect(self.address) self.connections.append(sock) # 返回鏈接池 return sock def __exit__(self, exc_ty, exc_val, tb): # 刪除關閉連接池中的對象 self.connections.pop().close() # Example use from functools import partial conn = LazyConnection(('www.python.org', 80)) with conn as s1: pass with conn as s2: pass # s1 and s2 are independent sockets ~~~
                  <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>

                              哎呀哎呀视频在线观看