f 字符串与 str.format() [英] f-strings vs str.format()

查看:67
本文介绍了f 字符串与 str.format()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 3.5 项目中经常使用 .format(),但我担心它会在下一个 Python 版本中被弃用,因为 f-strings,新的一种字符串文字.

<预><代码>>>>名称 = 测试">>>f我的应用程序名称是 {name}."我的应用程序名称是测试."

格式化字符串功能是否会完全取代旧的.format()?从现在开始,在所有情况下都使用新样式会更好吗?

我理解它基于简单胜于复杂"的理念.但是,性能问题呢?它们之间有什么区别吗?还是只是相同功能的简单外观?

解决方案

恐怕它会在下一个 Python 版本中被弃用

别这样,str.format 不会出现(也没有理由)很快离开,甚至引入了 fprefixed-strings 的 PEP在其摘要中声明:

<块引用>

本 PEP 不建议删除或弃用任何现有的字符串格式机制.

引入格式化字符串是为了解决其他格式化字符串方法的一些缺点;如果他们希望他们的代码适用于 Python 3.6+,不要扔掉旧方法并强迫天知道有多少项目使用 f-string.

<小时>

至于它们的性能,似乎我最初怀疑它们可能更慢是错误的,f-strings 似乎很容易胜过它们的 .format 对应物:

➜ cpython git:(master) ./python -m timeit -s "a = 'test'" "f'格式化字符串{a}'"500000 个循环,最好的 5 个:每个循环 628 纳秒➜ cpython git:(master) ./python -m timeit "'格式化字符串 {a}'.format(a='test')"100000 个循环,最好的 5 个:每个循环 2.03 微秒

在撰写本文时,这些是针对 CPython 存储库的 master 分支完成的;它们肯定会发生变化:

  • f-strings,作为一个新特性,可能会有优化
  • 对 CPython 的优化可能会使 .format 更快(例如 Speedup 方法调用 1.2x)

但实际上,不要太担心速度,要担心对您和其他人来说更易读的内容.

在许多情况下,这将是 f-strings,但是 在某些情况下 format 更好.

I'm using the .format() a lot in my Python 3.5 projects, but I'm afraid that it will be deprecated during the next Python versions because of f-strings, the new kind of string literal.

>>> name = "Test"
>>> f"My app name is {name}."
'My app name is Test.'

Does the formatted string feature come to fully replace the old .format()? And from now on, would it be better to use the new style in all cases?

I understand that it's based on the idea that "Simple is better than complex." However, what about performance issues; is there any difference between them? Or is it just a simple look of the same feature?

解决方案

I'm afraid that it will be deprecated during the next Python versions

Don't be, str.format does not appear (nor has a reason) to be leaving any time soon, the PEP that introduced fprefixed-strings even states in its Abstract:

This PEP does not propose to remove or deprecate any of the existing string formatting mechanisms.

Formatted strings were introduced to address some of the shortcomings other methods for formatting strings had; not to throw the old methods away and force god-knows how many projects to use f-string's if they want their code to work for Python 3.6+.


As for the performance of these, it seems my initial suspicion that they might be slower is wrong, f-strings seem to easily outperform their .format counterparts:

➜ cpython git:(master) ./python -m timeit -s "a = 'test'" "f'formatting a string {a}'"
500000 loops, best of 5: 628 nsec per loop
➜ cpython git:(master) ./python -m timeit "'formatting a string {a}'.format(a='test')"
100000 loops, best of 5: 2.03 usec per loop

These were done against the master branch of the CPython repository as of this writing; they are definitely subject to change:

  • f-strings, as a new feature, might have possible optimizations
  • Optimizations to CPython might make .format faster (e.g Speedup method calls 1.2x)

But really, don't worry about speed so much, worry about what is more readable to you and to others.

In many cases, that's going to be f-strings, but there's some cases where format is better.

这篇关于f 字符串与 str.format()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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