4

Possible Duplicate:
Tips on upgrading to python 3.0?

I am beginning Python and Python 3 is hardly a choice today. But I want the new code I write to have no problems running or being converted to Python 3. Are there any issues known that I should keep in mind for this?

Community
  • 1
  • 1
Ivan
  • 63,011
  • 101
  • 250
  • 382
  • 5
    Always run with the "-3" flag, and python2.6 should inform you of any issues that "2to3" won't be able to automatically translate. – Michael Aaron Safyan May 24 '10 at 10:53
  • Also, this is a duplicate of many other questions. For example: http://stackoverflow.com/questions/282819/is-it-worth-learning-python-2-6-with-3-0-coming, http://stackoverflow.com/questions/1072028/tips-on-upgrading-to-python-3-0 – Michael Aaron Safyan May 24 '10 at 10:55

1 Answers1

1

The full correct answer is in the comments, of course - but if you only do one thing to prepare for Python 3, make it learning to use parentheses with 'print'.

Python 2.x:

print 'Hello, World!'

Python 3.x:

print('Hello, World!')

It's the number one most common error in my code when I try to write Python 3.

(And since both methods work with 2.x, you might as well go ahead and get used to using the parens!)

Rini
  • 254
  • 1
  • 10
  • If you want to use print as a function (with parentheses) rather than as a statement (without parentheses) in Python 2.6 or earlier, you'll need to add this at the beginning of each module/file: `from __future__ import print_function` – blokeley May 24 '10 at 17:13
  • 1
    Actually, I believe it works fine as long as you are printing single values. It is only when printing multiple (comma-separated) values that the meaning of parentheses changes between Python 2 and 3. See http://www.python.org/dev/peps/pep-3105/#backwards-compatibility for full details. – Rini May 24 '10 at 21:50