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.
'%4d' % 42
vs
'{:4d}'.format(42)
It isn'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.
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.
.format() is mostly too verbose for my tastes. Even Guido himself put it into the "meh" category in a talk. To be clear, I do use it when I need quick lightweight templating, say building a very complicated string.
But for most simple output printf style is shorter, simpler and I know it by heart. There was a guy at work who insisted on using .format() for everything that soured me on it, sometimes even simple string concatenation:
Sometimes lines reached 200 chars. If lines were broken down, they might extend to 4 lines for each debug statement, adding tons of clutter. Said it was more readable because you could ignore the end. :/
As sago said, printf is more concise in the majority of use cases. Not to mention the logging niceties:
log.info('result: %s %s', one, two) # shorter and note that fmt is deferred!
A _big_ one in my eyes. Too bad logging doesn't support {} internally.
I've also wondered if .format could be improved if the % operator could be used with it, given appropriate precedence rules. Or perhaps use another char... dunno & or /?
Value reuse/positional parameter references could've been added easily to the old-style formatting in the same way POSIX added them to C's printf(), using the $ specifier (although printf() unusually indices from 1, not 0):
'%1$s %0$s' % ('one', 'two', )
Changing the padding character would probably not be too difficult either - just add the appropriate specifier to the formatting DSL; and I'm not so keen on supporting nested structures in the format string, as it seems to be going in the direction of allowing arbitrary expressions/Python code into a formatting DSL - one that I think shouldn't be more than a regular language.
I had to read your statement a couple of times to understand it. You're saying that the old syntax is totally incompatible with the new syntax? It's a shame, but I'm sure it was necessary.
The various other improvements are all things I've made use of in real code. Format is now a tool that you can reach for before you need to go to a more heavyweight templating lib (like Jinja2).
Most of the examples of the brevity of the old style seem to be quite unconcerned with actual formatting. Honestly I just do simple print "Hello: " + a when I'm not too picky, and use .format when I actually want a nice output; the time of the few extra chars in format is unimportant compared to the time it takes to choose good field widths for the first time anyway, and I find the old syntax more cryptic.
I almost always use format because it looks nicer, hence more readable. Shorter is not always better. It is also mandatory in i10n strings because if there are more than 2 arguments, they might be in different order in another language.
I've seen this and tried to avoid it since getting bitten my moving code around and not catching which variables were used. It might have been even more obscured with something like TEMPLATED_STRING.format(locals())
My last remaining complaint with new formatting is with Pylint and logging messages. The old-style way is to write:
logging.debug('some value: %d', 123)
which avoids the cost of string interpolation if that log message wasn't going to be emitted, say because the log level is higher than DEBUG. If you instead write:
logging.debug('some value: {}'.format(123))
then Pylint will complain that I "Use % formatting in logging functions but pass the % parameters as arguments (logging-format-interpolation)".
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.
I really don't mind these apparent (small) improvements to Python.
However, the fact that Python 3 just went off and did it's own things rather than the usual cycle of:
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
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.
I'm a c dinosaur, so I've always used old-style formatting because I know it off by heart. Having said that, the alignment operator (<^>) reminds me of my joy when learning python - power and simplicity!
Because we're turning everything into a string at the end of the day anyway!
Yeah. Having finished the article, .format() just isn't really needed. If I'm at the point where I'm doing templating with key:values, I'll be using jinja.
I personally prefer the old style still, and I still don'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.
• new, means, eventually we'll have to use it, so might as well get used to it now!
• it's a method on an object, which makes my brain happy. I never managed to get my brain wrapped around the old style syntax, it's inconsistent with the rest of the python syntax.
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.
[+] [-] sago|11 years ago|reply
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.
vs It isn'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.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.
[+] [-] mixmastamyk|11 years ago|reply
.format() is mostly too verbose for my tastes. Even Guido himself put it into the "meh" category in a talk. To be clear, I do use it when I need quick lightweight templating, say building a very complicated string.
But for most simple output printf style is shorter, simpler and I know it by heart. There was a guy at work who insisted on using .format() for everything that soured me on it, sometimes even simple string concatenation:
Sometimes lines reached 200 chars. If lines were broken down, they might extend to 4 lines for each debug statement, adding tons of clutter. Said it was more readable because you could ignore the end. :/As sago said, printf is more concise in the majority of use cases. Not to mention the logging niceties:
A _big_ one in my eyes. Too bad logging doesn't support {} internally.I've also wondered if .format could be improved if the % operator could be used with it, given appropriate precedence rules. Or perhaps use another char... dunno & or /?
Pycon, are you listening? ;)[+] [-] userbinator|11 years ago|reply
[+] [-] aidos|11 years ago|reply
The various other improvements are all things I've made use of in real code. Format is now a tool that you can reach for before you need to go to a more heavyweight templating lib (like Jinja2).
[+] [-] Trumpet6|11 years ago|reply
[+] [-] Grue3|11 years ago|reply
[+] [-] amelius|11 years ago|reply
'{}'.format(format_decimal_with_prepended_spaces(42, 4))
[+] [-] fragmede|11 years ago|reply
[+] [-] reidrac|11 years ago|reply
[+] [-] quarktasche|11 years ago|reply
[+] [-] pfranz|11 years ago|reply
[+] [-] wodenokoto|11 years ago|reply
[+] [-] maxerickson|11 years ago|reply
[+] [-] kstrauser|11 years ago|reply
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|11 years ago|reply
However, the fact that Python 3 just went off and did it's own things rather than the usual cycle of:
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.[+] [-] toyg|11 years ago|reply
[+] [-] kasabali|11 years ago|reply
improve a language feature & deprecate the old way of doing it -> Python 2.6+ and 3.2+
remove features that have been replaced -> 3.x
Did I also mention it is not hard to have a single code base that is compatible with both 2.x and 3.x?
[+] [-] infix|11 years ago|reply
-- The Zen of Python
[+] [-] erichurkman|11 years ago|reply
[+] [-] jkbr|11 years ago|reply
[+] [-] foxylad|11 years ago|reply
[+] [-] auscompgeek|11 years ago|reply
[+] [-] lumpypua|11 years ago|reply
[+] [-] civilian|11 years ago|reply
Yeah. Having finished the article, .format() just isn't really needed. If I'm at the point where I'm doing templating with key:values, I'll be using jinja.
[+] [-] jkbr|11 years ago|reply
[+] [-] mangeletti|11 years ago|reply
[+] [-] dorfsmay|11 years ago|reply
• it is supposedly significantly faster
• new, means, eventually we'll have to use it, so might as well get used to it now!
• it's a method on an object, which makes my brain happy. I never managed to get my brain wrapped around the old style syntax, it's inconsistent with the rest of the python syntax.
[+] [-] japaget|11 years ago|reply
[+] [-] huxley|11 years ago|reply
Since Centos 5 still has about 2 years of life but fewer and fewer Python packages support 2.4, it might be worth looking at Pyenv:
https://github.com/yyuu/pyenv
Good overview of what it lets you do:
http://fgimian.github.io/blog/2014/04/20/better-python-versi...
[+] [-] hobarrera|11 years ago|reply
Python 2.4 is actually pretty ancient. To put it into perspective, it's from when we had things Firefox 2.0, and IE6 was the latest IE.
[+] [-] zenogais|11 years ago|reply
[+] [-] jegutman|11 years ago|reply
basically locals(), vars(), and globals() I find very useful for string formatting.
[+] [-] gamesbrainiac|11 years ago|reply
Its good, but I think this is even better.
[+] [-] gcb0|11 years ago|reply
Why not 'field: {:center :minsize=8 varname}'.format(varname=123) ?
[+] [-] roywiggins|11 years ago|reply
[+] [-] father_of_two|11 years ago|reply
[+] [-] unknown|11 years ago|reply
[deleted]