views:

633

answers:

3

Hello,

I've got a problem with executing a python script in different environments with different versions of the interpreter, because the way text is formatted differ from one version to another.

In python < 2.6, it's done like this:

n = 3
print "%s * %s = %s" % (n, n, n*n)

whereas in python >= 2.6 the best way to do it is:

n = 3
print "{0} * {0} = {1}".format(n, n*n)

But how about when you want the script to be runned in any python version? What's better, to write python<2.6 code to assure instant compatibility or use the python>=2.6 code that is going to be the way it's used in the future?

Is there any other option to write code for the actual python versions without loosing compatibility with olders?

Thanks

+3  A: 

str.format() was introduced in Python 2.6, but its only become the preferred method of string formatting in Python 3.0.

In Python 2.6 both methods will still work, of course.

It all depends on who the consumers of your code will be. If you expect the majority of your users will not be using Python 3.0, then stick with the old formatting.

However, if there is some must-have feature in Python 3.0 that you require for your project, then its not unreasonable to require your users to use a specific version of the interpreter. Keep in mind though that Python 3.0 breaks some pre 3.0 code.

So in summary, if you're targeting Python 3.0, use str.format(). If you're targeting Pyhon <3.0, use % formatting.

Andre Miller
Considering he's using print as a statement, he's probably targeting Python < 3, so sticking with the percent operator should be fine.
balpha
Yes, you are correct, in Python 3 you would need to use the print() function instead.
Andre Miller
Well, the second code is intended to be used in python 2.6, if I wanted to port the code to Python 3 it would be easy with 2to3 tool.The main problem is with text formatting... but I think I'm going to use the python<2.6 code by now...
willehr
I am interested to find in some ad-hoc testing that my speed GOES DOWN in python 2.6 when I choose to use the New Shiny 'string'.format() way. And people wonder why Py3 adoption proceeds at a glacial pace. Python 2.5 actually runs many bits of my code faster than 2.6, and much faster than 2.5. YMMV.
Warren P
+1  A: 

What about the good old tuple or string concatenation?

print n, "*", n, "=", n*n
#or
print str(n) + " * " + str(n) + " = " + str(n*n)

it's not as fancy, but should work in any version.

if it's too verbose, maybe a custom function could help:

def format(fmt, values, sep="%%"):
   return "".join(str(j) for i in zip(fmt.split(sep), values) for j in i)

#usage
format("%% * %% = %%", [n, n, n*n])
fortran
Yes, it's an option, but quite ugly when passing text as a function argument with 3 or 4 variables to replace...
willehr
A problem with string concatenation is that it makes localization significantly more difficult. If you want to translate to another language, first problem is that the natural order of variables might be different, and second, it introduces more strings to translate, which is even harder since you have only fragments of strings. Translators will not see how each string is used, only the fragments in the localization files. If you expect them to read the source to deduce how it should be translated, you will have a revolt!
TokenMacGuy
A: 

You should have a look at the string.Template (link for 3.1) way to format a string, the API is stable across all versions >=2.5. I think that concatenation is the simple way to achieve portability for both old and new Python versions. Obviously it's slow, verbose and less pythonic than alternatives.

thomas