无聊之人--除了技术,还是技术,你懂得
分类: Python/Ruby
2011-08-28 21:22:16
8.6. Dictionary-based string formatting
Why did you learn about locals and globals? So you can learn about dictionary-based string formatting. As you recall, regular string formatting provides an easy way to insert values into strings. Values are listed in a tuple and inserted in order into the string in place of each formatting marker. While this is efficient, it is not always the easiest code to read, especially when multiple values are being inserted. You can't simply scan through the string in one pass and understand what the result will be; you're constantly switching between reading the string and reading the tuple of values.
你为什么学习局部变量和全局变量?这样你就可以学习基于字典的字符串格式化。正如你所知道的,正则表达式的字符串格式化提供一种简单的方式来向字符串中插入变量值.。tuple中的值被显示出来,然后按顺序插入带有字符串标识的字符串中。尽管种方法是很有效率的,但是当你打算插入多个值的时候,读入它们却不是那么容易。你不能简单的对字符串进行一次扫描来确定你的结果。你需要经常在读取字符串以及读取tuple中的值之间进行转换。
There is an alternative form of string formatting that uses dictionaries instead of tuples of values.
还存在另一种字符串格式化的方式:使用字典而不是使用tuple中的值。
Example 8.13. Introducing dictionary-based string formatting
例8.13 基于字典的字符串格式化简介
Instead of a tuple of explicit values, this form of string formatting uses a dictionary, params. And instead of a simple %s marker in the string, the marker contains a name in parentheses. This name is used as a key in the params dictionary and subsitutes the corresponding value, secret, in place of the %(pwd)s marker. 不是显式的使用tuple的值,这种形式的字符串格式化使用了一个字典,params。在字符串中不是简单的使用%占位符,该占位符包含一个用括号包围的名字。该名字被用作params字典中的key值,然后用来取代响应的值,悄悄的,取代%(pwd)s 占位符。 |
|
Dictionary-based string formatting works with any number of named keys. Each key must exist in the given dictionary, or the formatting will fail with a KeyError. 基于字典的字符串格式化适用于任意数量的命名的key。每一个key都必须存在于给定的字典中,否则该格式化将以抛出KeyError而结束。 |
|
You can even specify the same key twice; each occurrence will be replaced with the same value. 甚至你可以两次使用同一个键:该键每一次替换都使用相同值。 |
So why would you use dictionary-based string formatting? Well, it does seem like overkill to set up a dictionary of keys and values simply to do string formatting in the next line; it's really most useful when you happen to have a dictionary of meaningful keys and values already. Like locals.
那你为什么使用基于字符串的格式化呢?哦,这看起来有点矫枉过正,即为了在下一行代码中简单的使用字符串格式户而专门建立一个键值对组成的字典;当你刚好有一个键和值很有意义的字典时,使用这种方式是很有用的。如locals函数
Example 8.14. Dictionary-based string formatting in BaseHTMLProcessor.py
例8.14 BaseHTMLProcessor.py中基于字符串的格式化
def handle_comment(self, text):
self.pieces.append("" % locals())
Using the built-in locals function is the most common use of dictionary-based string formatting. It means that you can use the names of local variables within your string (in this case, text, which was passed to the class method as an argument) and each named variable will be replaced by its value. If text is 'Begin page footer', the string formatting "" % locals() will resolve to the string ''. 使用内置的locals函数是使用基于字典的字符串格式化最常的使用方式。它意味着你可以在你的字符串中(在本例中,text,作为一个参数传递给类方法)使用本地变量的名字,同时每一个命名变量都会被它的值所替代。如果text的值是Begin page footer’,那么字符串格式化"" % locals() 会被解析成字符串''. |
Example 8.15. More dictionary-based string formatting
例8.15 更多基于字典的字符串格式化
When this method is called, attrs is a list of key/value tuples, just like the items of a dictionary, which means you can use multi-variable assignment to iterate through it. This should be a familiar pattern by now, but there's a lot going on here, so let's break it down: 当该方法被调用的时候,attrs是一个键值对组成的tuple列表,也就是一个items的字典,这意味着你可以使用多个变量同时赋值来完成对字典的迭代。现在对你来说,这是一种非常熟悉的模式,但是这里还有很多要解释的,我们将它分解开来说: a. Suppose attrs is [('href', 'index.html'), ('title', 'Go to home page')]. b. 假定attrs的内容是:[('href', 'index.html'), ('title', 'Go to home page')] c. In the first round of the list comprehension, key will get 'href', and value will get 'index.html'. d. 在对列表进行第一次的迭代中,key的值为‘href’,value的值为’index.html’ e. The string formatting ' %s="%s"' % (key, value) will resolve to ' href="index.html"'. This string becomes the first element of the list comprehension's return value. f. 格式化字符串' %s="%s"' % (key, value)将会解析成href="index.html"。这个字符串成了列表复合操作返回值的第一个元素 g. In the second round, key will get 'title', and value will get 'Go to home page'. h. 在第二次迭代中,key =’title’,value=’go to home page’. i. The string formatting will resolve to ' title="Go to home page"'. j. 格式化字符串将会解析成title="Go to home page k. The list comprehension returns a list of these two resolved strings, and strattrs will join both elements of this list together to form ' href="index.html" title="Go to home page"'. l. 列表的复合操作返回解析两个解析字符串所组成的列表,然后 strattrs将这些列表元素连接起来得到最终的形式: href="index.html" title="Go to home page。 |
|
|
||
Now, using dictionary-based string formatting, you insert the value of tag and strattrs into a string. So if tag is 'a', the final result would be '', and that is what gets appended to self.pieces. 现在使用基于字典的字符串格式化,你将一个标签的值以插入strattrs中。因此如果标签时’a’,最终的结果将会是<'',接着将得到的字符串追加到self.pieces. |
|
|
||
|
|
|
||
|
Using dictionary-based string formatting with locals is a convenient way of making complex string formatting expressions more readable, but it comes with a price. There is a slight performance hit in making the call to locals, since locals builds a copy of the local namespace. 使用基于字典的字符串格式化来操纵locals函数是一种很便捷的方法,它可以让复杂的字符串格式化表达式更加可读,但是它也是有代价的。因为locals创建的是本地命名空间的copy,因此为了调用locals会对性能产生一定的影响。 performance hit 性能影响(google,) |
|
||
|