3 ms·
I thought % formatting was the "old way" and we're supposed to use format() in Python 3. Strange that they're adding % support to bytes now.
by chc 11y ago
I thought % formatting was the "old way" and we're supposed to use format() in Python 3. Strange that they're adding % support to bytes now.
- nhumrich 11y agoIf you read the PEP, it explains how they are adding it for easier python2 -> python3 migration
- lambda 11y agoAs long as % formatting still works, it is in many ways more convenient that format. '"Foo: %s" % bar' is a lot shorter to write than '"Foo: {}".format(bar)'. And % formatting is still built into logger, so if you use logger heavily you will probably be used to it. While the format() mini-language is nicer in a lot of ways, the convenience of % and its ubiquity mean that I generally use it more often. The main time when I reach for format() is when I need more detailed control of column widths and the like, which you can do with things like "{var:{width}}".format(var=var, width=width)
- kbd 11y agoPersonally I dislike % formatting even if it's a few characters shorter to type. .format is superior in every other way and has consistent-with-everything-else method call syntax. Edit: I think it's largely the special flower overloading of the % operator that bugs me the most. If % was spelled .sprintf I'd like it more.
- deleted 11y ago[deleted]
- kbd 11y agoThanks for bringing this up. I asked about this on reddit yesterday[1] and was linked the thread where they originally discussed this[2]. Basically, the goal is to make porting from 2 to 3 easier, and there's also a notion that printf-style formatting may be more natural than format-style formatting for bytes. I still wish they were consistent and provided a .format method for bytes, especially since, as you point out, .format was supposed to be the "new way" to do formatting and this feels like a step backwards. And what about backward compatibility for porting Python 2 code that used .format to do formatting on what are semantically byte strings? [1] https://www.reddit.com/r/Python/comments/3c7lne/python_350b3_is_out/cstcpg7 https://www.reddit.com/r/Python/comments/3c7lne/python_350b3... [2] http://thread.gmane.org/gmane.comp.python.devel/144712/focus=144856 http://thread.gmane.org/gmane.comp.python.devel/144712/focus...