Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

>>So let me reiterate my suggestion for Python 3 in web applications: don't use it just yet, but write your code in a way that it could pass through 2to3.

Is there a good article for summarizing how I write Python2 code that is compatible Python3? I mean, what things do I avoid in Python2 to write good Python3 code? (And while I'm asking dumb questions: how do I make blocks of text italic on HN?)



* Read the summary of changes in Python 3: http://docs.python.org/release/3.0.1/whatsnew/3.0.html

* See Guido Rossum's Transitional Development notes here: http://www.artima.com/weblogs/viewpost.jsp?thread=208549

* Read Mark Pilgrim's excellent chapter from Dive into Python 3: http://diveintopython3.org/porting-code-to-python-3-with-2to...

* Use Python 2.7 and turn on deprecation warnings.

EDIT: Found this great guide to forward compatibility:

* http://www.slideshare.net/regebro/python-3-compatibility-pyc...


Best resource I know if is to look at what the 2to3 tool does and try to minimize those items. Many items it fixes are possible to do correctly in 2.6+ (e.g. print(x) instead of print x, even if print still doesn't evaluate as an expression in 2.x).

http://docs.python.org/library/2to3.html#to3-fixers


You can use:

  from __future__ import print_function
And then print() should work as expected in Python 2.6+. That is, print(1, 2, 3) will print '1 2 3' instead of '(1, 2, 3)'.


And while I'm asking dumb questions: how do I make blocks of text italic on HN?

Use *

There is a help link in the right-bottom corner of the text field if you press edit.


I am not aware of such resource, but the best thing to do IMO is to simply include 2t3 and running your test suite on both 2.x and 3.x code from day one.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: