A Print function for different versions of python

Gribouillis 1 Tallied Votes 873 Views Share

This snippet defines a Print function which can be used with python 2.4, 2.5, 2.6 and 3.0 (for 3.0 this is just the built-in print function). It's interface is that of the python 3.0's print. It simplifies the task of writing transitional code which runs under python 2.5 and python 3.0.

# module cbprint
# (C) 2008 Gribouillis at www.daniweb.com

"""This module defines a Print function to use with python 2.x or 3.x.

Usage:
  from cbprint import Print
  Print("hello", "world")

It's interface is that of python 3.0's print. See
http://docs.python.org/3.0/library/functions.html?highlight=print#print
"""
__all__ = ["Print"]
import sys
try:
  Print = eval("print") # python 3.0 case
except SyntaxError:
  try:
    D = dict()
    exec("from __future__ import print_function\np=print", D)
    Print = D["p"] # 2.6 case
    del D
  except SyntaxError:
    del D
    def Print(*args, **kwd): # 2.4, 2.5, define our own Print function
      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"))

Dani AI

Generated

A few practical notes tied to the original cbprint idea from and the follow-ups by @leegeorg07 and .

The cbprint helper is a useful historical hack for mixed 2.x/3.x environments, but Python 2 has reached end of life (January 1, 2020). When control of the runtime is possible, migrating to Python 3 and using the native print() is the safest long‑term choice.

When true cross‑version support is required, prefer a maintained compatibility library instead of mutating builtins (the latter was suggested by and works, but it is brittle and discouraged). The widely used six module gives a ready wrapper that behaves like Python 3’s print and supports sep, end, and file. Install with pip and call its print_ wrapper:

pip install six
from six import print_

print_("hello", "world", sep=" - ", end="\n")

For projects that must still run on very old interpreters, keep any compatibility helper local to the project and import it explicitly from a small module rather than exposing it globally. That answers @leegeorg07’s question about “making it built‑in” in a safer way: explicit import at module boundaries is more maintainable than changing interpreter globals.

A simple migration workflow that has worked in many codebases: run the test suite under Python 3, use automated porting tools (2to3 / modernize / futurize) to find likely issues, add a compatibility layer like six while both runtimes are supported, and finally remove the shim once the codebase runs only on Python 3. The cbprint snippet remains a neat minimal solution for constrained environments, but for production code prefer the library/porting route.

Member Avatar for Member #361407
Member #361407

where would you put this to have it as a built in function?
i have python 2.5 but it says python2.4.6 if that helps

martineau 19 Newbie Poster

@leegeorg07, you could make it built-in for the rest of the execution of your script by making it a part of the __builtins__ module by adding these lines at the end:

    import __builtin__   # use "builtins" in Python 3
    __builtin__.Print = Print

Although this practice is often frowned-up.

Gribouillis commented: good idea +13
Lucaci Andrew 140 Za s|n

@martineau you are knew to our forums, but we have a rule going on here: if the thread is older than 3 months, there's no need to resurface it. I'm not saying that your advice is bad. I'm just saying that @leegeorg07 posted his question 4 years ago, and I doubt he's still searching for an answer to it.

martineau 19 Newbie Poster

@Lucaci-Andrew, Yes I'm new to the forums and knew that @leegeorg07's message was old, but thought a reply was worthwhile mainly for any other folks who might stumble across the original code snippet and related thread as I myself did.

Lucaci Andrew commented: touché +6
Lucaci Andrew 140 Za s|n

As for my mistake, early in the morning.
As for your reply: this can be easily solved searching it through {insert your preferred search engine}.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.