Python 3.0 comes with a builtin print function which breaks old code. In order to write code wich is compatible both with the 2.x series and with the 3.x series, I'm using the following Print function

import sys
def Print(*values, **kwd):
  fout = kwd.get("file", sys.stdout)
  sep = kwd.get("sep", " ")
  end = kwd.get("end", "\n")
  w = fout.write
  if values:
    w(str(values[0]))
    for v in values[1:]:
      w(sep)
      w(str(v))
  w(end)
if sys.version_info[0] >= 3:
  Print = eval("print")

In my programs, I use this Print function as if it was 3.0's print. The difference is that it still runs with python 2.5, like in this example

Print("Hello",  "world", sep="--", end=" !\n")

What do you you think of this ? Is there a better thing to do ?

You could also get the python version
print sys.version_info
and then use the appropriate "print input_var" or "print(input_var)" depending on whether version is > 2.5.

As I understand it, the 2.x print statement syntax is illegal in 2.6 when the new print function is desired, and always illegal syntax in 3.0. (I think that's what #1 meant when he said "breaks old code"). So logic along the lines of

if sys.version_info[0] >= 3:
    print(a,b,c)
else:
    print a,b,c

... wouldn't fly in 3.0 or 2.6 with new print. It's like a crutch to get from 2.5 to 2.6 new-print, and if that's the case I say "good going". I may start converting my current code to use this now while other, braver souls are taking 2.6/3.0 on their shakedown cruises.

Good point. Time to download 3.0 and do a search of comp.lang.python. It has undoubtedly been discussed. For now, I will have to form the habit of using
print("hello world")
as that works in both 2.5 and 3.0.

Good point. Time to download 3.0 and do a search of comp.lang.python. It has undoubtedly been discussed. For now, I will have to form the habit of using
print("hello world")
as that works in both 2.5 and 3.0.

Unfortunately

print("hello", "world")

doesn't work as expected in 2.5 :=)
I modified the Print function above to get a version which should work with 2.4, 2.5, 2.6 and 3.0

# module cbprint
# Defines a Print function to use with python 2.x or 3.x
__all__ = ["Print"]
import sys
try:
  Print = eval("print")
except SyntaxError:
  try:
    D = dict()
    exec("from __future__ import print_function\np=print", D)
    Print = D["p"]
    del D
  except SyntaxError:
    del D
    def Print(*args, **kwd):
      fout = kwd.get("file", sys.stdout)
      w = fout.write
      if args:
        w(str(args[0]))
        sep = kwd.get("sep", " ")
        for a in args[1:]:
          w(sep)
          w(str(a))
      w(kwd.get("end", "\n"))

Usage (all versions, tested on 2.5 and 3.0):

from cbprint import Print
Print("Hello", "world")

The problem is that many existing libraries might be unable to run with python 3.0 for a while, and the default python implementation on many systems (like linux) may still be python 2.5 for a while. This happened in the past with python 1.5. So I think we should be concerned with writing code which works both for 2.5 and 3.0.

This article has been dead for over six months. Start a new discussion instead.