A Print function for different versions of python

Gribouillis Gribouillis is offline Offline Dec 20th, 2008, 3:10 pm |
0
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.
Quick reply to this message  
Python Syntax
  1. # module cbprint
  2. # (C) 2008 Gribouillis at www.daniweb.com
  3.  
  4. """This module defines a Print function to use with python 2.x or 3.x.
  5.  
  6. Usage:
  7. from cbprint import Print
  8. Print("hello", "world")
  9.  
  10. It's interface is that of python 3.0's print. See
  11. http://docs.python.org/3.0/library/functions.html?highlight=print#print
  12. """
  13. __all__ = ["Print"]
  14. import sys
  15. try:
  16. Print = eval("print") # python 3.0 case
  17. except SyntaxError:
  18. try:
  19. D = dict()
  20. exec("from __future__ import print_function\np=print", D)
  21. Print = D["p"] # 2.6 case
  22. del D
  23. except SyntaxError:
  24. del D
  25. def Print(*args, **kwd): # 2.4, 2.5, define our own Print function
  26. fout = kwd.get("file", sys.stdout)
  27. w = fout.write
  28. if args:
  29. w(str(args[0]))
  30. sep = kwd.get("sep", " ")
  31. for a in args[1:]:
  32. w(sep)
  33. w(str(a))
  34. w(kwd.get("end", "\n"))
0
leegeorg07 leegeorg07 is offline Offline | Feb 8th, 2009
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
 
 

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC