为什么 Python 中有这么多格式风格?
Why are there so many formatting flavours in Python?
我最近看了一个 Python 教程,其中作者一直在玩弄各种 Python 字符串格式化表达式,我被一个想法打动了:
为什么 Python 中有这么多字符串格式风格?
- 有基于 C 语言
printf
的方法(顺便说一句,在各种来源中,它被引用为已弃用并计划删除,但在标准 Python 中有大量示例文档 (!)).
- 另一方面,有
C#
派生的 .format()
处理字符串的方式。
- 最后,随着 Python 3.6 的出现,我们得到了
formatted f-string literal
,即 f-string
。
- 有些还包括
string
模块,但这对我来说似乎有点过分了。
我发现 here,上述前两种技术既不同又相互重叠,具体取决于用途。
最后但并非最不重要的一点是,这种丰富的方法是否与 import this
中 Python 的声明之一相矛盾?
There should be one-- and preferably only one --obvious way to do it.
如果我错了请纠正我,但这本质上是 "un-Pythonic" 在某种程度上。
读者能否就此提出自己的想法,最好能提供一些相关示例作为支持?
https://docs.python.org/2/whatsnew/2.6.html#pep-3101-advanced-string-formatting
看来是要让字符串格式化的功能更强大
来自上面的link:
"In Python 3.0, the % operator is supplemented by a more powerful string formatting method, format(). Support for the str.format() method has been backported to Python 2.6."
这就是进化的样子。
"We need string concatenation".
当然可以。 "hello" + ''' ''' + 'world'
或者,如果你愿意,' '.join(['hello', 'world'])
"But "found " + str(var) + " matches"
is kind of clumsy. I hear Lisp has princ
...?"
哦好的,这里:'found %i matches' % var
(...时间流逝...)
"I hear you are overhauling the entire language and making print
into a function. Gasp! Can we also fix this string formatting thing, it's pretty crazy after all? Newbies don't understand why you need parentheses when you interpolate more than one variable, and really, overriding the modulo operator for strings was a pretty wacky thing to do."
嗯,你是对的,好的,在这里。 'found {0} matches'.format(var)
是的,让我们摆脱这种不直观的复杂性。我们不希望模数成为一个特例,它很不优雅。
"Thanks. But... wait, there's really a lot of code which uses the old syntax, and when you compare them, the new syntax is really clunky."
唉。我们可以让它不那么笨拙。来点语法糖吧! f'found {var} matches'
还有更多来源!
"Mmmm, I dunno. The modulo is still easier to type."
你快把我逼疯了。好的,你喜欢就玩吧。不过别说我没提醒你
我最近看了一个 Python 教程,其中作者一直在玩弄各种 Python 字符串格式化表达式,我被一个想法打动了:
为什么 Python 中有这么多字符串格式风格?
- 有基于 C 语言
printf
的方法(顺便说一句,在各种来源中,它被引用为已弃用并计划删除,但在标准 Python 中有大量示例文档 (!)). - 另一方面,有
C#
派生的.format()
处理字符串的方式。 - 最后,随着 Python 3.6 的出现,我们得到了
formatted f-string literal
,即f-string
。 - 有些还包括
string
模块,但这对我来说似乎有点过分了。
我发现 here,上述前两种技术既不同又相互重叠,具体取决于用途。
最后但并非最不重要的一点是,这种丰富的方法是否与 import this
中 Python 的声明之一相矛盾?
There should be one-- and preferably only one --obvious way to do it.
如果我错了请纠正我,但这本质上是 "un-Pythonic" 在某种程度上。
读者能否就此提出自己的想法,最好能提供一些相关示例作为支持?
https://docs.python.org/2/whatsnew/2.6.html#pep-3101-advanced-string-formatting
看来是要让字符串格式化的功能更强大
来自上面的link: "In Python 3.0, the % operator is supplemented by a more powerful string formatting method, format(). Support for the str.format() method has been backported to Python 2.6."
这就是进化的样子。
"We need string concatenation".
当然可以。 "hello" + ''' ''' + 'world'
或者,如果你愿意,' '.join(['hello', 'world'])
"But
"found " + str(var) + " matches"
is kind of clumsy. I hear Lisp hasprinc
...?"
哦好的,这里:'found %i matches' % var
(...时间流逝...)
"I hear you are overhauling the entire language and making
嗯,你是对的,好的,在这里。 'found {0} matches'.format(var)
是的,让我们摆脱这种不直观的复杂性。我们不希望模数成为一个特例,它很不优雅。
"Thanks. But... wait, there's really a lot of code which uses the old syntax, and when you compare them, the new syntax is really clunky."
唉。我们可以让它不那么笨拙。来点语法糖吧! f'found {var} matches'
还有更多来源!
"Mmmm, I dunno. The modulo is still easier to type."
你快把我逼疯了。好的,你喜欢就玩吧。不过别说我没提醒你