<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 功能強大 支持多語言、二開方便! 廣告
                # 8.6.?基于 dictionary 的字符串格式化 為什么學習 `locals` 和 `globals`?因為接下來就可以學習關于基于 dictionary 的字符串格式化。或許您還能記起,[字符串格式化](../native_data_types/formatting_strings.html "3.5.?格式化字符串")提供了一種將值插入字符串中的一種便捷的方法。值被列在一個 tuple 中,按照順序插入到字符串中每個格式化標記所在的位置上。盡管這種做法效率高,但還不是最容易閱讀的代碼,特別是當插入多個值的時候。僅用眼看一遍字符串,您不能馬上就明白結果是什么;您需要經常地在字符串和值的 tuple 之間進行反復查看。 有另外一種字符串格式化的形式,它使用 dictionary 而不是值的 tuple。 ## 例?8.13.?基于 dictionary 的字符串格式化介紹 ``` >>> params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"} >>> "%(pwd)s" % params 'secret' >>> "%(pwd)s is not a good password for %(uid)s" % params 'secret is not a good password for sa' >>> "%(database)s of mind, %(database)s of body" % params 'master of mind, master of body' ``` | | | | --- | --- | | \[1\] | 這種字符串格式化形式不用顯式的值的 tuple,而是使用一個 dictionary,`params`。并且標記也不是在字符串中的一個簡單 `%s`,而是包含了一個用括號包圍起來的名字。這個名字是 `params` dictionary 中的一個鍵字,所以 `%(pwd)s` 標記被替換成相應的值 `secret`。 | | \[2\] | 基于 dictionary 的字符串格式化可用于任意數量的有名的鍵字。每個鍵字必須在一個給定的 dictionary 中存在,否則這個格式化操作將失敗并引發一個 `KeyError` 的異常。 | | \[3\] | 您甚至可以兩次指定同一鍵字,每個鍵字出現之處將被同一個值所替換。 | 那么為什么您偏要使用基于 dictionary 的字符串格式化呢?的確,僅為了進行字符串格式化,就事先創建一個有鍵字和值的 dictionary 看上去的確有些小題大作。它的真正最大用處是當您碰巧已經有了像 [`locals`](locals_and_globals.html "8.5.?locals 和 globals") 一樣的有意義的鍵字和值的 dictionary 的時候。 ## 例?8.14.?`BaseHTMLProcessor.py` 中的基于 dictionary 的字符串格式化 ``` def handle_comment(self, text): self.pieces.append("<!--%(text)s-->" % locals()) ``` | | | | --- | --- | | \[1\] | 使用內置的 `locals` 函數是最普通的基于 dictionary 的字符串格式化的應用。這就是說您可以在您的字符串 (本例中是 `text`,它作為一個參數傳遞給類方法) 中使用局部變量的名字,并且每個命名的變量將會被它的值替換。如果 `text` 是 `'Begin page footer'`,字符串格式化 `"&lt;!--%(text)s--&gt;" % locals()` 將得到字符串 `'&lt;!--Begin page footer--&gt;'`。 | ## 例?8.15.?基于 dictionary 的字符串格式化的更多內容 ``` def unknown_starttag(self, tag, attrs): strattrs = "".join([' %s="%s"' % (key, value) for key, value in attrs]) self.pieces.append("<%(tag)s%(strattrs)s>" % locals()) ``` | | | | --- | --- | | \[1\] | 當這個模塊被調用時,`attrs` 是一個鍵/值 tuple 的 list,就像一個 [dictionary 的 `items`](../native_data_types/mapping_lists.html#odbchelper.items "例?3.25.?keys, values 和 items 函數")。這就意味著我們可以使用[多變量賦值](../native_data_types/declaring_variables.html#odbchelper.multiassign "3.4.2.?一次賦多值")來遍歷它。到現在這將是一種熟悉的模式,但是這里有很多東西,讓我們分開來看:1\. 假設 `attrs` 是 `[('href', 'index.html'), ('title', 'Go to home page')]`。2\. 在這個列表解析的第一輪循環中,`key` 將為 `'href'`,`value` 將為 `'index.html'`。3\. 字符串格式化 `'?%s="%s"' % (key, value)` 將生成 `'?href="index.html"'`。這個字符串就作為這個列表解析返回值的第一個元素。4\. 在第二輪中,`key` 將為 `'title'`,`value` 將為 `'Go to home page'`。5\. 字符串格式化將生成 `' title="Go to home page"'`。6\. 這個 list 理解返回兩個生成的字符串 list,并且 `strattrs` 將把這個 list 的兩個元素連接在一起形成 `'?href="index.html" title="Go to home page"'`。 | | \[2\] | 現在,使用基于 dictionary 的字符串格式化,我們將 `tag` 和 `strattrs` 的值插入到一個字符串中。所以,如果 `tag` 是 `'a'`,最終的結果會是 `'&lt;a href="index.html" title="Go to home page"&gt;'`,并且這就是追加到 `self.pieces` 后面的東西。 | > 重要 > 使用 `locals` 來應用基于 dictionary 的字符串格式化是一種方便的作法,它可以使復雜的字符串格式化表達式更易讀。但它需要花費一定的代價。在調用 `locals` 方面有一點性能上的問題,這是由于 [`locals` 創建了局部名字空間的一個拷貝](locals_and_globals.html#dialect.locals.readonly.example "例?8.12.?locals 是只讀的,globals 不是")引起的。
                  <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>

                              哎呀哎呀视频在线观看