On Fri, 17 May 2013 21:18:15 +0300, Carlos Nepomuceno wrote: > I thought there would be a call to format method by "'%d\n' % i". It > seems the % operator is a lot faster than format. I just stopped using > it because I read it was going to be deprecated. :( Why replace such a > great and fast operator by a slow method? I mean, why format is been > preferred over %?
That is one of the most annoying, pernicious myths about Python, probably second only to "the GIL makes Python slow" (it actually makes it fast). String formatting with % is not deprecated. It will not be deprecated, at least not until Python 4000. The string format() method has a few advantages: it is more powerful, consistent and flexible, but it is significantly slower. Probably the biggest disadvantage to % formatting, and probably the main reason why it is "discouraged", is that it treats tuples specially. Consider if x is an arbitrary object, and you call "%s" % x: py> "%s" % 23 # works '23' py> "%s" % [23, 42] # works '[23, 42]' and so on for *almost* any object. But if x is a tuple, strange things happen: py> "%s" % (23,) # tuple with one item looks like an int '23' py> "%s" % (23, 42) # tuple with two items fails Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: not all arguments converted during string formatting So when dealing with arbitrary objects that you cannot predict what they are, it is better to use format. -- Steven -- http://mail.python.org/mailman/listinfo/python-list