[原文]P112
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.
There is an alternative form of string formatting that uses dictionaries instead of tuples of values
[pope改进译]
为什么你要学习locals 和 globals呢?因为你要学习基于字符串设计的字典类型。就像你看到的那样,在此字符串格式化提供了一种在字符中嵌入变量的简单方式。值被listed到tuple中,并且 在有序的字符串中将要求每个要匹配内容使用格式符替代。很好这是高效的,虽然并不是最便于阅读的,特别是当多个值被替换后。你不能简单的查看字符串一次就理解结果是什么;你需要经常在字符串和tuple值之间反复阅读。
另外一种字符串格式化的选择是使用字典方式替代tuple。
-------------------------------------------------------------------------------
[?]嘻嘻不好意思问一句,这个each formatting marker是什么?
[pope译]为什么你要学习locals 和 globals呢?因为你要学习基于字符串设计的字典类型。就像你看到的那样,在此正则字符格式提供了一种在字符中嵌入变量的简单方式。
值被listed到tuple中,并且 在有序的字符串中将要求每个要匹配内容使用格式符替代。
[?]好好查一下吧,到底是什么才说你经常在读字符串和读tuple的值之间切换呢?
[pope译] 很好这是高效的,虽然并不是最便于阅读的,特别是当多个值被替换后。你不能简单的查看字符串一次就理解结果是什么;你需要经常在字符串和tuple值之间反复阅读。
[net译 来源:http://woodpecker.org.cn/diveintopython/html_processing/dictionary_based_string_formatting.html]
为什么学习 locals 和 globals?因为接下来就可以学习关于基于
dictionary 的字符串格式化。或许您还能记起,字符串格式化提供了一种将值插入字符串中的一种便捷的方法。值被列在一个
tuple
中,按照顺序插入到字符串中每个格式化标记所在的位置上。尽管这种做法效率高,但还不是最容易阅读的代码,特别是当插入多个值的时候。仅用眼看一遍字符串,您不能马上就明白结果是什么;您需要经常地在字符串和值的
tuple 之间进行反复查看。
有另外一种字符串格式化的形式,它使用 dictionary 而不是值的 tuple。
[popexizhi:
regular string formatting
[pope译]正则字符格式
[net 译] 字符串格式化
[popexizhi] 这个位置有交叉引用的,是定位到string formatting 的,并且 regular 是 规则的,均匀的,而正则表达式应该是 regular expressions .也不能这么确定就翻译的有问题,因为formatting 表达方式其实到底是不是正则的一种元素使用呢?再看看吧!:)
[?]好好查一下吧,到底是什么才说你经常在读字符串和读tuple的值之间切换呢?
[popexizhi:这里说的应该是元字符表达式看上去不是一目了然,要查看者自己找答案。]
]
没有评论:
发表评论