TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

PyFormat – Practical examples of old and new style string formatting

159 点作者 hgezim大约 10 年前

18 条评论

sago大约 10 年前
A good reference.<p>My take home is that every single version that is possible to do in old style formatting is longer in new style formatting. Even more so if the old-style formatting were written without the extraneous 1-ples.<p><pre><code> &#x27;%4d&#x27; % 42 </code></pre> vs<p><pre><code> &#x27;{:4d}&#x27;.format(42) </code></pre> It isn&#x27;t surprising that so much code is still being written in old-style. The advantages of the new style, like padding with arbitrary characters; value reuse (particularly for I18n, though that tends to use a different mechanism for production systems that need it); and nested display; are really quite minor.<p>This feeds into the other python issue on HN today - why does py3 have so little uptake? Because it is a solution to a problem few people have.
评论 #9384747 未加载
评论 #9385640 未加载
评论 #9384684 未加载
评论 #9385021 未加载
评论 #9387514 未加载
评论 #9384740 未加载
fragmede大约 10 年前
They missed my favorite trick! locals() will give you a dict of, well, local variables, which generally coincides with what you want, so:<p><pre><code> a = 4 &#x27;A is: {a}&#x27;.format(**locals()) </code></pre> works as expected.
评论 #9386598 未加载
评论 #9384521 未加载
评论 #9386166 未加载
评论 #9385598 未加载
评论 #9385148 未加载
kstrauser大约 10 年前
My last remaining complaint with new formatting is with Pylint and logging messages. The old-style way is to write:<p><pre><code> logging.debug(&#x27;some value: %d&#x27;, 123) </code></pre> which avoids the cost of string interpolation if that log message wasn&#x27;t going to be emitted, say because the log level is higher than DEBUG. If you instead write:<p><pre><code> logging.debug(&#x27;some value: {}&#x27;.format(123)) </code></pre> then Pylint will complain that I &quot;Use % formatting in logging functions but pass the % parameters as arguments (logging-format-interpolation)&quot;.<p>Yes, I can disable that particular warning, either by maintain a pylintrc file or adding annotations to the source code. Yes, this is a dumb complaint. But yes, it still bugs me.
andy_ppp大约 10 年前
I really don&#x27;t mind these apparent (small) improvements to Python.<p>However, the fact that Python 3 just went off and did it&#x27;s own things rather than the usual cycle of:<p><pre><code> 1) improve a language feature 2) deprecate the old way of doing it 3) give people time to update code (usually a couple of point releases) 4) remove features that have been replaced </code></pre> Python 3 should not have dropped lots of little changes with no backwards compatibility. They should still make 2.8 and 2.9 that are releases that remove features and add new ones until most python code works in Python 3.
评论 #9386845 未加载
评论 #9387815 未加载
infix大约 10 年前
There should be two-- and preferably only two --obvious ways to do it.<p>-- The Zen of Python
erichurkman大约 10 年前
Python 2.7 also added a non-locale aware thousands separator that be be combined with other options:<p><pre><code> &gt;&gt;&gt; &quot;{:&gt;40,.2f}&quot;.format(sys.maxint) &#x27; 9,223,372,036,854,775,808.00&#x27;</code></pre>
jkbr大约 10 年前
<p><pre><code> Basic formatting Old: &#x27;%d %d&#x27; % (1, 2) New: &#x27;{} {}&#x27;.format(1, 2) </code></pre> It should rather be `{:d} {:d}&#x27;.format(1, 2)` but even that isn&#x27;t strictly equivalent (try both styles with a float or a Decimal).
foxylad大约 10 年前
I&#x27;m a c dinosaur, so I&#x27;ve always used old-style formatting because I know it off by heart. Having said that, the alignment operator (&lt;^&gt;) reminds me of my joy when learning python - power and simplicity!
auscompgeek大约 10 年前
This fails to mention the str.format_map() shortcut method (new in Python 3.2). It&#x27;s useful if you already have a dictionary of all your values!
lumpypua大约 10 年前
Really cool and the site looks great too! Was a css framework used for the styling or is it hand written? Can&#x27;t tell from the minification.
civilian大约 10 年前
<p><pre><code> &#x27;%d %d&#x27; % (1, 2) </code></pre> I would have done this as<p><pre><code> &#x27;%s %s&#x27; % (1, 2) </code></pre> Because we&#x27;re turning everything into a string at the end of the day anyway!<p>Yeah. Having finished the article, .format() just isn&#x27;t really needed. If I&#x27;m at the point where I&#x27;m doing templating with key:values, I&#x27;ll be using jinja.
评论 #9384553 未加载
mangeletti大约 10 年前
I personally prefer the old style still, and I still don&#x27;t see the value of deprecating it in favor of the new style. I started Python in 2008 (p3k was brand new), and I used new style formatting for about 2 years. After realizing that I was the only one using new style I switched and now I cannot imagines going back.
评论 #9385068 未加载
japaget大约 10 年前
One caution that this document does not mention is that the new formatting is available only in recent versions of Python. One of my projects runs on a CentOS 5.x server, which has Python 2.4, so I had to convert all of the new style formatting to old in order to get it to run there.
评论 #9384402 未加载
评论 #9387027 未加载
zenogais大约 10 年前
Can&#x27;t tell you how many times I&#x27;ve Googled this and had to dig through the docs. Thank you!
jegutman大约 10 年前
I use the format: foo = &#x27;foo&#x27; bar = &#x27;bar&#x27; print &quot;%(foo)s%(bar)s&quot; % locals() which I guess is what this is similar too.<p>basically locals(), vars(), and globals() I find very useful for string formatting.
gamesbrainiac大约 10 年前
In Dash.app (on Mac), there&#x27;s actually a nifty file that you can download that shows you how all the different formatting options work.<p>Its good, but I think this is even better.
gcb0大约 10 年前
Its very lame that the new format kept the bad design from C hacks.<p>Why not &#x27;field: {:center :minsize=8 varname}&#x27;.format(varname=123) ?
评论 #9386329 未加载
father_of_two大约 10 年前
Hey, very nice. I knew about the &quot;new&quot; format() but didn&#x27;t know it was so powerful.