传递元组列表时格式说明符如何取值

how format specifier taking value while tuple list is passed

我有一段代码如下:

tupvalue = [('html', 96), ('css', 115), ('map', 82)]

因此,在为特定索引以所需格式打印上述元组时,我发现了这样的代码:

>>> '%s:%d' % tupvalue[0]
'html:96'

我想知道格式说明符 '%s:%d' 如何将单个值 tupvalue[0] 识别为两个值的元组?请使用文档参考解释此机制。

如何使用推导式将 tupvalue 中的所有值格式化为示例中所示的所需格式?

首先,简单的问题:

How can I use a comprehension to format all the values in tupvalue in the required format as in the example shown?

这是一个列表理解:['%s:%d' % t for t in tupvalue]

现在,更难的问题!

how the single value tupvalue[0] is recognised as a tuple of two values by the format specifier '%s:%d'?

您的直觉是正确的,这里发生了一些奇怪的事情。 元组在语言中是特殊大小写的,用于字符串格式化。

>>> '%s:%d' % ('css', 115)  # tuple is seen as two elements
'css:115'
>>> '%s:%d' % ['css', 115]  # list is just seen as one object!
TypeError: not enough arguments for format string

百分比风格的字符串格式没有正确地鸭式。所以,如果你真的想格式化一个元组,你必须将它包装在另一个元组中,这与任何其他类型的对象不同:

>>> '%s' % []
'[]'
>>> '%s' % ((),)
'()'
>>> '%s' % ()
TypeError: not enough arguments for format string

文档的相关部分位于 4.7.2. printf-style String Formatting 部分,其中提到:

If format requires a single argument, values may be a single non-tuple object. Otherwise, values must be a tuple with exactly the number of items specified by the format string

元组的奇怪处理是文档该部分开头注释中提到的怪癖之一,也是推荐使用较新的字符串格式化方法 str.format 的原因之一。

请注意,字符串格式的处理发生在 运行时。您可以使用抽象语法树来验证这一点:

>>> import ast
>>> ast.dump(ast.parse('"%s" % val'))
"Module(body=[Expr(value=BinOp(left=Str(s='%s'), op=Mod(), right=Name(id='val', ctx=Load())))])"

'%s' % val 解析为 '%s'val 上的二进制操作,其处理方式与 str.__mod__(val) 类似,在 CPython 中是 BINARY_MODULO 操作码。这意味着通常由 str 类型来决定当收到的 val 不正确时要做什么 *,这只会在表达式为 [=57] 时发生=]evaluated,即一旦解释器到达该行。因此,val 是错误类型还是 few/too 元素太多并不重要 - 这是运行时错误,而不是语法错误。

除了在某些特殊情况下,CPython 的窥孔优化器能够在编译时 "constant fold" 它。

* 除非val的类型子类str,在这种情况下type(val).__rmod__ 能够控制结果。