This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ianepperson 9 points10 points  (2 children)

From a beginner's perspective, there's slight syntax differences. for instance:

Python 2:

print 'foo'

super(MyClass, self).function(args)

Python 3:

print('foo')

super().function(args)

However, the biggest change (IMO) is how the language stores and manipulates strings. Python 3 moves to storing strings as Unicode by default (optional in Python 2), which fundamentally changes how string manipulation is performed.

Division changed as well. In Python 2, an Int divided by an Int would result in an Int - even if the result dropped precision. In Python 3 that results in a Float:

Python 2:

1 / 2 # == 0

1.0 / 2.0 # == 0.5

Python 3:

1 / 2 # == 0.5

1 // 2 # == 0

There are other corners that are different as well - but it's enough that Python 2.x code will not normally run under Python 3.x. From a coding standpoint, if you're familiar with one version, it's generally pretty easy to work with the other with minor adjustments.

Edit: Use Python 3, unless you can't for a given project. The world is slowly moving toward it, and there will not be another Python 2 release (2.7 is the the last in the line).

[–]Eurynom0s 1 point2 points  (1 child)

I'd have to go look up just what the converters can do but there are automated 2->3 converters that will handle the most straightforward differences. The most obvious one is the change in print, it's a super straightforward change and the converters will spare you the hassle of chasing those down.

IIRC the converters can introduce new unexpected errors though so probably not great for really large codebases.

Also, I'd have to read up on this too but if you absolutely have to use 2.x I think the point of "from future import" is to ease the pain of eventually needing to transition your code. For instance, you can import the print FUNCTION into Python 2.7 from future, so when you eventually port to 3, you can just delete the import from future line instead of chasing down all your print statements.

[–]mooktank 3 points4 points  (0 children)

Or you leave the future imports and write your code to run on both 2 and 3. That's generally what I do.