8

Pep 3101 provides a rationale for ultimately replacing the % operator with the format method. This question and the accepted answer dwell on the same points.

I could not find, however, a rationale for the new syntax and I don't understand the benefits of the change. The pep 3101 lists various alternate syntaxes which also included the famous printf style of format specifiers as noted in the C99 standard and it's variants. (For a documentation example go here to section 7.19.6.1 "The fprintf function" on pages 274ff).

For the new string.format() method it was considered to reuse the same format specification language that has been in use by the % operator.

What can be done with the new syntax that could not have been done with the old?

Edit: Parameter reordering could also have been added to the old syntax in the same way it has been added to the ANSI C standard. See any recent man sprintf

Community
  • 1
  • 1
cfi
  • 10,915
  • 8
  • 57
  • 103
  • If this bothers you, use a different library, or use python 2.7. – Marcin Mar 23 '12 at 14:16
  • I stated that it does not bother me. The Python development process is actually a very open process, and even if Guido does overrides he usually comments. So I'm just curious. – cfi Mar 23 '12 at 14:17
  • 2
    I have to say, I can't find a suitable description anywhere of `"%2$*1$d"` as a format specifier. What does it do, and where is `$` defined? – Chowlett Mar 23 '12 at 14:17
  • 1
    @Chowlett: `man sprintf` as I mentioned. [Look here](http://linux.die.net/man/3/sprintf) – cfi Mar 23 '12 at 14:20
  • Because sprintf's syntax is fugly, especially when reordering or named elements are involved. And also always requires the type. – Cat Plus Plus Mar 23 '12 at 14:25
  • That said, both printf-like and new formatting is supported in Python 3, and the former isn't going anywhere. – Cat Plus Plus Mar 23 '12 at 14:26
  • @Marcin Many PEPs include a rationale and a discussion of the , which usually gives pretty good motivation. For instance, PEP 414 explains at great length why anyone would want the `u` prefix back and who's supposed to benefit from that. Yes, one could check `python-dev` too, but if you want to critizise lack of research, there are far better canidates. –  Mar 23 '12 at 14:30
  • @delnan It seems that you consider doing *one piece of research that turns out to be a dead end* to be enough research to justify asking stackoverflow to *read the minds of people who can be asked directly*. I don't. – Marcin Mar 23 '12 at 14:33
  • 4
    I don't understand what the fuss is about. This is a question that may or may not have a good answer. Perhaps the OP was expecting an answer like "the new format allows you to do X, which is objectively better". An answer like that may not exist, but how could he tell without asking? – Eduardo Ivanec Mar 23 '12 at 14:39
  • 1
    @Marcin, et al.: I have browsed through the relevant parts of the years 2006 & 2007 of the discussions on the Python dev list. To me it just seems the discussions just started with "here's this new language proposal". I don't see any particular link that would be beneficial for others to help research for an answer. However, as I have pointed out - and maybe Eduardo agrees with this - sometimes these questions are relevant in raising the level of understanding of a programming language. How would you suggest to maybe rephrase that this Q would be more acceptable? – cfi Mar 23 '12 at 15:03
  • I think it's mostly numeric indexing. If you need to internationalize code it's important and was only a recent addiction to the standard printf.() – Martin Beckett Mar 23 '12 at 15:04
  • @cfi I suggest that you don't rephrase it, and take it over to the python-developers mailing list. – Marcin Mar 23 '12 at 15:06
  • @Martin, but the numeric indexing is in the C99 standard already? The Pep3101 discussion happened mostly in 2006/7 it seems- arguing about it's closeness to .NET. (e.g. [here](http://grokbase.com/p/python/python-3000/0645yq03ce/string-formating-operations-in-python-3k)) – cfi Mar 23 '12 at 15:17
  • @Martin: One can use named format groups like `"%(test)d" % { 'test' : 3 }`, so that's not a very good argument. – Niklas B. Mar 23 '12 at 15:19
  • 5
    If you can reduce your question to the part about the benefits, you *might* have a question, but everything else is just asking for opinions, whereas there's a limited subset of people who can *actually* answer the question with authority. That said, it's NC. If you have to address in your question all the debate that's going on in the comments, it's typically a good indicator that your question is NC. – casperOne Mar 23 '12 at 15:29
  • I thought (now I don't think so) the reason was obvious --the desire to provide an intuitive syntax for string formatting: 1. the number of C# developers trumps the number of Python and C developers combined. 2. "There is no intuitive interface, not even the nipple. It's all learned." 3. Conclusion: to provide an intuitive interface use the interface the most people got accustomed to. – jfs Mar 23 '12 at 17:47
  • 1
    @casperOne: Interestingly, in the comments only one user (Marcin) actually voiced his concerns about the kind of question. Strictly speaking I thus should not have addressed this in an edit of my question. Most of the comments (mine not included in the count) actually try to address the question. This is probably because it's closed and no one can answer. – cfi Mar 24 '12 at 09:05
  • @casperOne: "whereas there's a limited subset of people who can actually answer the question with authority". Erm. Are we to ask questions only if there are more than x people who can answer it? Ever asked a question where one of the Tcl or Python or Git or Django, or ... developers answered himself? Stackoverflow is a site of great users. I would dare to ask Q's even if I cannot imagine anyone to answer it. And how can I know how many people have actively followed the dev lists without posting? But this is a discussion for meta... – cfi Mar 24 '12 at 09:07
  • @casperOne: The closure of this question just seems even more hilarious to me. See [this question and the reactions](http://stackoverflow.com/a/1569007/923794). And [the number of people being able to answer a question is not a criteria for closing a question](http://meta.stackexchange.com/a/126760/170949). – cfi Mar 25 '12 at 09:29
  • @casperOne: you apparently forgot to close as not constructive the many duplicates of this question. See for instance this one (which has more than 80 upvotes) http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format – MarcH Feb 20 '13 at 11:05
  • @MarcH You could always flag those for moderator attention. – casperOne Feb 20 '13 at 12:32
  • @MarcH: Thanks for pointing out the possible duplicate! The problem I have with that question is that it actually asks multiple different topics. I believe my Q is more to the point. – cfi Feb 20 '13 at 12:48
  • The ignorance of the majority of Python users of the capabilities of the old syntax is appalling. I got a feeling that this might be the reason for most people closing these kind of questions as being not constructive. Possibly this should have been a discussion on the Python dev mailing list and for the pep at that time. – cfi Feb 20 '13 at 12:50

0 Answers0