用元组作为参数的新样式格式 [英] New style formatting with tuple as argument

查看:198
本文介绍了用元组作为参数的新样式格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能使用元组作为formatter的参数(string.format())?它在旧样式(字符串%)中工作正常吗?

这个代码工作:

 >>>元组=(500000,500,5)
...打印第一项:%d,第二项:%d和第三项:%d。 %元组

第一项:500000,第二项:500和第三项:5.



$ p
$ b

 >>> tuple =(500000,500,5)
... print(First item:{:d},second item:{:d} and third item:{:d}。
.. .format(tuple))

Traceback(最近一次调用的最后一个):
在< module>中的第2行< stdin>
ValueError:类型为'str'的对象的未知格式代码'd'

{!r}

 >>> tuple =(500000,500,5)
... print(First item:{!r},second item:{!r} and third item:{!r}。
.. .format(tuple))

Traceback(最近一次调用的最后一个):
在< module>中的第2行< stdin>
IndexError:元组索引超出范围

虽然这样工作:

 >>>打印(第一项:{!r},第二项:{!r}和第三项:{!r}
... .format(500000,500,50))

第一项:500000,第二项:500,第三项:5.


解决方案

格式化的旧方法使用二元运算符。就其性质而言,它只能接受两个论点。格式化的新方法使用一种方法。方法可以使用任意数量的参数。

由于您有时需要传递多个格式的东西,而且一直创建一个元素的元组有点笨拙,风格的方式提出了一个黑客:如果你把它作为一个元组传递,它将使用元组的内容作为格式化的东西。如果你传递了一个元组以外的东西,它将使用它作为唯一的格式。



新方法不需要这样的破解:因为它是一个方法,它可以采取任何数量的参数。因此,需要将多个格式的内容作为单独的参数传递。幸运的是,你可以使用 * 将一个元组解压缩为参数:

  print (第一项:{:d},第二项:{:d}和第三项:{:d}。。格式(* tuple))


Why can't I use tuple as argument to formatter in new style ("string".format())? It works fine in old style ("string" %)?

This code works:

>>> tuple = (500000, 500, 5)
... print "First item: %d, second item: %d and third item: %d." % tuple

    First item: 500000, second item: 500 and third item: 5.

And this doesn't:

>>> tuple = (500000, 500, 5)
... print("First item: {:d}, second item: {:d} and third item: {:d}."
...       .format(tuple))

    Traceback (most recent call last):
     File "<stdin>", line 2, in <module>
    ValueError: Unknown format code 'd' for object of type 'str'

Even with {!r}

>>> tuple = (500000, 500, 5)
... print("First item: {!r}, second item: {!r} and third item: {!r}."
...       .format(tuple))

    Traceback (most recent call last):
     File "<stdin>", line 2, in <module>
    IndexError: tuple index out of range

Though it works with that way:

>>> print("First item: {!r}, second item: {!r} and third item: {!r}."
...       .format(500000, 500, 50))

    First item: 500000, second item: 500 and third item: 5.

解决方案

The old way of formatting used a binary operator, %. By its nature, it can only accept two arguments. The new way of formatting uses a method. Methods can take any number of arguments.

Since you sometimes need to pass multiple things to format and it's somewhat clumsy to create tuples with one item all the time, the old-style way came up with a hack: if you pass it as a tuple, it will use the contents of the tuple as the things to format. If you pass it something other than a tuple, it will use that as the only thing to format.

The new way does not need such a hack: as it is a method, it can take any number of arguments. As such, multiple things to format will need to be passed as separate arguments. Luckily, you can unpack a tuple into arguments using *:

print("First item: {:d}, second item: {:d} and third item: {:d}.".format(*tuple))

这篇关于用元组作为参数的新样式格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆