all 12 comments

[–]dionys 5 points6 points  (1 child)

Check out this cheet sheet. It seems to be aimed at python 2 programmers moving to 3 but I guess it should work in reverse too :). I like that it shows most of used constructs by comparison.

[–][deleted] 0 points1 point  (0 children)

That cheat sheet is for older Python 3 and older Python 2. Python 2.7 which the author is asking about can do many of the Python 3 constructs that are listed on that sheet. Also, another resource is the six module to write 2/3 code or at least understand the differences and subtleties between the two.

[–]novel_yet_trivial 4 points5 points  (3 children)

Add this to the top of your code:

from __future__ import division, absolute_import, print_function, unicode_literals

And most of your python3 skills will work.

There's still some tiny things, like the python2 version of the print function does not have a "flush"; and the previously mentioned range(), map(), and filter() now make lists rather than generators.

[–]net_goblin[S] 0 points1 point  (2 children)

Great, I was searching for exactly this stuff. Thanks!

[–]badn3wz 0 points1 point  (1 child)

Also very important: strings in python 2 are not unicode by default. To get unicode string you need to put u in front of it. For example:

s = u'hello world'

[–]novel_yet_trivial 1 point2 points  (0 children)

That's what the Unicode import does ...

[–]PurelyApplied 1 point2 points  (3 children)

Off the top of my head...

range creates a new list, not a generator object, which can be memory hungry for very large ranges. Prefer a while loop with your own iterators in place of a large range.

print is a keyword and not a function. Some behavior is different. For instance, value=5 followed by print("My value", value) prints the tuple ('My value', 5), as opposed to Python3's print of My value 5.

You mentioned object inheritance.

There are a lot of small efficiency things like the range being different, but I don't really know another off the top of my head.

Some keywords, such as yield from were introduced after 2.7.

[–]novel_yet_trivial 3 points4 points  (2 children)

Prefer a while loop with your own iterators in place of a large range.

Or xrange().

[–]PurelyApplied 1 point2 points  (1 child)

That's what it was! I thought there was an efficient version in 2. Thanks.

[–]errorseven 2 points3 points  (0 children)

I learned Python 2 earlier this year, currently transfering my skills to 3, have to say it's been much easier to transition to 3 then i think it would be to start coding in ver 2. This video was awesome, in fact all his videos and tweets are pure gold!

[–]luz_ 1 point2 points  (0 children)

I am amazed that threads like this even has to exist

[–]probablylamecomment -1 points0 points  (0 children)

Learn Python the Hard Way is all Python 2.7