为什么Python有一个格式化函数和一个格式化方法

编程入门 行业动态 更新时间:2024-10-22 23:02:11
本文介绍了为什么Python有一个格式化函数和一个格式化方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

格式内嵌的函数看起来像是 str.format 方法专门用于格式化单个对象的情况。

例如。

>>>格式(13,'x')'d'

p>

>>> '{0:x}'。格式(13)'d'

和IMO它看起来更好,但为什么不在每种情况下使用 str.format 来使事情更简单?这两个都是在 2.6 中引入的,所以一定要有一个很好的理由,一个是什么?

编辑:我询问 str.format 和格式,而不是为什么我们没有(13).format

解决方案

格式和 str.format 做不同的事情。尽管你可以使用 str.format 来代替两者,但是有独立的版本。

顶层格式函数是所有对象支持的新格式化协议的一部分。它只是调用它传递的对象的 __格式__ 方法,并返回一个字符串。这是一个低级任务,Python的风格通常是为这些内置的函数。 Paulo Scardine的答案解释了一些基本原理,但我不认为它真的解决了格式和 str.format do。

str.format 方法稍微高一点,有点复杂它不仅可以将多个对象格式化为单个结果,还可以对对象进行重新排序,重复,索引和其他各种转换。不要只考虑{}。format(obj)。 str.format 确实是为复杂任务设计的,如下所示:

<$ c $格式(obj0,obj1)#重新排序,重复和调用obj1上的repr {0.value:。{0.precision} f格式(obj)#使用obj的attrs作为值和格式spec {obj [name]}。format(obj = my_dict)#通过关键字获取参数,并执行一个项目查找

对于每个项目的低级格式, str.format $ b 虽然({+ format_code +}).format(obj)保证给出相同的结果作为格式(obj,format_code),我怀疑后者会快一点,因为它不需要解析格式字符串来检查任何复杂的东西。然而,在真正的程序中,开销可能会在噪声中丢失。

当涉及到使用(包括Stack Overflow示例)时,您可能会看到更多<$ c $因为一些程序员不知道格式,这既新又相当模糊。相反,很难避免 str.format (除非你决定坚持使用%运算符你的格式)。因此,理解 str.format 调用的方便程度(对于您和您的同行)可能会超过任何性能方面的考虑。

The format function in builtins seems to be like a subset of the str.format method used specifically for the case of a formatting a single object.

eg.

>>> format(13, 'x') 'd'

is apparently preferred over

>>> '{0:x}'.format(13) 'd'

and IMO it does look nicer, but why not just use str.format in every case to make things simpler? Both of these were introduced in 2.6 so there must be a good reason for having both at once, what is it?

Edit: I was asking about str.format and format, not why we don't have a (13).format

解决方案

I think format and str.format do different things. Even though you could use str.format for both, it makes sense to have separate versions.

The top level format function is part of the new "formatting protocol" that all objects support. It simply calls the __format__ method of the object it is passed, and returns a string. This is a low-level task, and Python's style is to usually have builtin functions for those. Paulo Scardine's answer explains some of the rationale for this, but I don't think it really addresses the differences between what format and str.format do.

The str.format method is a bit more high-level, and also a bit more complex. It can not only format multiple objects into a single result, but it can also reorder, repeat, index, and do various other transformations on the objects. Don't just think of "{}".format(obj). str.format is really designed for more about complicated tasks, like these:

"{1} {0} {1!r}".format(obj0, obj1) # reorders, repeats, and and calls repr on obj1 "{0.value:.{0.precision}f}".format(obj) # uses attrs of obj for value and format spec "{obj[name]}".format(obj=my_dict) # takes argument by keyword, and does an item lookup

For the low-level formatting of each item, str.format relies on the same machinery of the format protocol, so it can focus its own efforts on the higher level stuff. I doubt it actually calls the builtin format, rather than its arguments' __format__ methods, but that's an implementation detail.

While ("{"+format_code+"}").format(obj) is guaranteed to give the same results as format(obj, format_code), I suspect the latter will be a bit faster, since it doesn't need to parse the format string to check for any of the complicated stuff. However the overhead may be lost in the noise in a real program.

When it comes to usage (including examples on Stack Overflow), you may see more str.format use simply because some programmers do not know about format, which is both new and fairly obscure. In contrast, it's hard to avoid str.format (unless you have decided to stick with the % operator for all of your formatting). So, the ease (for you and your fellow programmers) of understanding a str.format call may outweigh any performance considerations.

更多推荐

为什么Python有一个格式化函数和一个格式化方法

本文发布于:2023-11-08 15:55:35,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   有一个   方法   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!