migrating from python 3 to 2

i just blogged about switching back to python 2 for development, and then today someone posted to their blog about python 2 to 3 migration.

so the following is partly tongue-in-cheek, but i thought i might post a little guide for anyone migrating to python 2: some of the pros and cons, and answer a few questions.

first of all: is it difficult?

i dont recommend migrating all of your projects from python 3 to 2. although it should be pretty easy, this is a theory vs. practice thing, and it probably isnt worth time migrating existing projects until there is a real need. (i would say the same for migrating in the other direction.)

for the most part, let need (and convenience) rather than hype or peer pressure be your guide here. you might try doing your next project with python 2 instead of 3, and see if it makes your project easier.

if so, you may find (as a former nokia developer i knew did) that theres no reason to forsake python 2 for 3– but ymmv. certainly python 3 wouldnt be here if it were useless to everyone. the push to standardize however, has shown (in my opinion) that python 3 is overrated, and its usefulness overestimated.

how is the library support for python 2?

library support will be mixed, as a number of libraries have moved to support python 3. the advice here would be to treat the two versions like different languages– if you need a library that is only supported by python 3, then you have the choice of a different library (where possible) or to use python 3.

which libraries will continue to be supported in python 2? perhaps a good guess would be the ones that are already supported in pypy– more about that later.

what is coding in python 2 like, vs. 3?

of course it depends on your goals. python 2 is supposed to have inferior support for unicode; hence the push in python3 to standardize on it. but not everything in computing is actually unicode; whether you use 2 or 3, there are conversions back and forth. personally, i find the conversions in python 2 less brittle, and the ones in python 3 more verbose, tedious and less predictable.

to sweeten the deal, you dont have to put parentheses in the print statement:

print “hello world– pretty cool, huh?”

if only theyd thought of that back in the days of python 3, eh?

of course you will hear stories from both camps– 3 is so much easier for unicode, or 2 is so much easier for strings– it should be obvious which camp im in. give python 2 a try, and if it doesnt feel like everything just got a lot easier, dont migrate! use what works for you.

isnt the python foundation dropping support for python 2?

yes, they are. in fact they were going to drop it last year! but they extended it to 2020. after that, you will have to use an unsupported interpreter or switch to pypy. pypy is an alternative implementation of the python interpreter– you should be able to run many of your existing python 2 programs with zero modifications.

library support for pypy is not exactly as broad as it is for python 2. for example: my own python project– fig– uses pygame optionally, but i have not succeeded in getting pygame to work in pypy. im told its possible, and ive tried. tips are welcome. ive used pygame in both python 2 and 3. python 3 support for pygame is pretty much “build it yourself,” if youre using debian/devuan stable. (and ive built it in devuan.)

how do i do float division?

this is one thing that python 3 definitely got right– 3/7 is .43857, not 0. the trick to make float division work in python 2, is to explicitly make either the numerator or denominator a float:

x = 3/7 # gives 0

x = float(3)/7 # gives .43857142857142855

x = 3/float(7) # gives the same as float(3)/7

it also works with variables on the right side.

how do i print on the same line without a space added after it?

there are probably multiple ways to do this, i prefer sys.stdout:

from sys import stdout

stdout.write(“hello”) ; stdout.flush()

the chr() command wont do codes over 255; how do i do unicode?

you can use unichr instead, or even overload the chr() command:

def chr(p): return unichr(p)

# you can also use unicode strings:

print u”\u2588″

more tips are available as “python 2 to 3” migration guides; to some degree they work in both directions.

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s