Python 3 str.format()

325 views
Skip to first unread message

Daniel Swarbrick

unread,
Aug 24, 2012, 11:56:01 AM8/24/12
to django-d...@googlegroups.com
Hi folks,

Apologies in advance if this topic has already been raised. I don't believe I've seen it on the list since I've been subscribed.

Since Django 1.5 has set the minimum version of Python at 2.6, and in conjunction with the push to make Django more Python 3 compatible, should we slowly start migrating to the new format string [1] syntax? The Python docs state that it is the new standard in Python 3, and should be preferred to the old %-style formatting. The %-style was supposed to be deprecated in Python 3.1. Whilst I'm a little vague on whether it's been officially deprecated yet, it is slated to be removed from Python altogether at some point.

The new str.format() method is supported by Python 2.6 onwards.

1: http://docs.python.org/library/string.html#format-string-syntax

Carl Meyer

unread,
Aug 24, 2012, 12:12:15 PM8/24/12
to django-d...@googlegroups.com
Hi Daniel,

On 08/24/2012 09:56 AM, Daniel Swarbrick wrote:
> Since Django 1.5 has set the minimum version of Python at 2.6, and in
> conjunction with the push to make Django more Python 3 compatible,
> should we slowly start migrating to the new format string [1] syntax?
> The Python docs state that it is the new standard in Python 3, and
> should be preferred to the old %-style formatting. The %-style was
> supposed to be deprecated in Python 3.1. Whilst I'm a little vague on
> whether it's been officially deprecated yet, it is slated to be removed
> from Python altogether at some point.

Can you link to where in the current docs it specifies that %-formatting
is deprecated and/or will be removed? I can't even find, on a cursory
search, where it says the new .format() style should be preferred.

I'm asking because I've seen Python core devs state quite strongly on
python-dev that there are no plans for deprecating or removing
%-formatting in Python 3. See e.g.
http://mail.python.org/pipermail/python-dev/2012-February/116800.html
and ensuing thread.

Given that, I'm not sure it's worth any effort at all to transition the
Django codebase (and as long as it does what we need, it may be best to
stick with %-formatting in new code as well, just to maintain consistency).

Carl

signature.asc

Daniel Swarbrick

unread,
Aug 24, 2012, 12:30:51 PM8/24/12
to django-d...@googlegroups.com
On 24 August 2012 18:12, Carl Meyer <ca...@oddbird.net> wrote:
> Can you link to where in the current docs it specifies that %-formatting
> is deprecated and/or will be removed? I can't even find, on a cursory
> search, where it says the new .format() style should be preferred.

It's not easy to find - I've only found the mention of the impending
deprecation in the Python 3.0 "What's New" docs -
http://docs.python.org/release/3.0/whatsnew/3.0.html#pep-3101-a-new-approach-to-string-formatting

However, I can't find followup references to that in the Python 3.1
docs. Maybe the decision was rescinded? Personally I didn't really see
anything majorly wrong with the %-style formatting. Being a C
developer myself, it was nice to have some common ground and not have
to go hunting for format string specifications.

This section http://docs.python.org/library/stdtypes.html#str.format
states "This method of string formatting is the new standard in Python
3, and should be preferred to the % formatting described in String
Formatting Operations in new code."

I just thought that if the %-style formatting is indeed earmarked for
removal, maybe we should start using the new format sooner rather than
later.

Ian Kelly

unread,
Aug 24, 2012, 1:11:49 PM8/24/12
to django-d...@googlegroups.com
This post by Guido is informative on the subject, even though it's
nearly three years old now:

http://mail.python.org/pipermail/python-dev/2009-September/092399.html

The 3.2 docs specifically say, "As the new String Formatting syntax is
more flexible and handles tuples and dictionaries naturally, it is
recommended for new code. However, there are no current plans to
deprecate printf-style formatting." The 3.3 docs no longer have this
quote, but they also indicate nothing about planned deprecation or
removal.

Until the Python developers announce an actual timeline for removal
(if they ever do), I can't see any reason for Django to be concerned
about which formatting approach to use, apart from the immediate
concerns of style and efficiency.

Cheers,
Ian

Alex Ogier

unread,
Aug 24, 2012, 1:56:36 PM8/24/12
to django-d...@googlegroups.com
> Until the Python developers announce an actual timeline for removal
> (if they ever do), I can't see any reason for Django to be concerned
> about which formatting approach to use, apart from the immediate
> concerns of style and efficiency.

I don't think Python will ever remove %-style formatting, even in a
hypothetical "Python 4," because there are a lot of APIs (including
Django's) that expose it. Guido mentions the logging package, but
there are also a lot of third party packages that take %-style
template strings as arguments to formatting functions. As a result,
the %-style syntax will have to stick around until most of these
packages convert over, and if the latest Python 3 documentation has
even stopped recommending str.format() as a better alternative then I
expect it to stick around for good.

Best,
Alex

claudep

unread,
Aug 24, 2012, 4:47:45 PM8/24/12
to django-d...@googlegroups.com

Le vendredi 24 août 2012 19:11:49 UTC+2, Ian a écrit :
Until the Python developers announce an actual timeline for removal
(if they ever do), I can't see any reason for Django to be concerned
about which formatting approach to use, apart from the immediate
concerns of style and efficiency.

One more reason not to adopt too quickly this syntax is missing support from gettext.

http://savannah.gnu.org/bugs/?30854

Claude

Daniel Swarbrick

unread,
Aug 24, 2012, 5:29:28 PM8/24/12
to django-d...@googlegroups.com

On 24/08/12 22:47, claudep wrote:
>
> One more reason not to adopt too quickly this syntax is missing
> support from gettext.
>
> http://savannah.gnu.org/bugs/?30854
>
Right. That makes up my mind then. I can't afford to be without gettext
support.

Until gettext supports the newer format, and until I see more of that
format in the wild, I'll keep using the printf-style.
Reply all
Reply to author
Forward
0 new messages