| | |
A Print function for different versions of python
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
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"))
Similar Threads
- python can't pass arguements from a function to another function? (Python)
- python 3 print() (Python)
- python versions (Python)
- Python print (Python)
- Having 2 versions of Python (Python)
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied alarm aliased basic beginner casino character code corners cursor definedlines development dictionary digital dynamic editing error events examples excel exe file filename float format ftp function graphics gui handling homework ideas iframe import input java line linux list lists logging loop matching mouse newb number numbers numeric output parameters parsing path port prime program programming progressbar projects py py2exe pygame pygtk pyqt pysimplewizard python random recursion recursive return reverse schedule scrolledtext searchingfile shebang skinning ssh statistics stdout string strings table tails terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode urllib urllib2 variable voip windows wxpython



