4/4/2016 - 10:00 AM

Python 2.7 vs Python 3

Python 2.7 vs Python 3

# Source -

Python 3 assumes that files are UTF-8 by default, Python 2 does not. We thus add # -*- coding: utf-8 to the top of our file

Native Python 2.7 strings are ASCII, while Python 3 strings are 32-bit unicode, which is far more desirable (as Unicode allows for over one million character symbols including Chinese and Latin accents, while ASCII is limited to 128). 

To force Python 2.7 strings to behave like unicode, we need only add the following import to the top of our file: from __future__ import unicode_literals.

Django provides a decorator that will effectively use our implementation of __str__ to create a __unicode__ when running the project in Python 2.7. We thus import the python_2_unicode_compatible decorator and apply it to our class.

# Source -

Some of the major changes scheduled for Python 3.0 were:

Changing print so that it is a built-in function, not a statement. This made it easier to change a module to use a different print function, as well as making the syntax more regular. In Python 2.6 and 2.7 print() is available as a builtin but is masked by the print statement syntax, which can be disabled by entering from __future__ import print_function at the top of the file. [22]

Moving reduce (but not map or filter) out of the built-in namespace and into functools (the rationale being that operations using reduce are expressed more clearly using an accumulation loop);[23]

Adding support for optional function annotations that can be used for informal type declarations or other purposes;[24]

Unifying the str/unicode types, representing text, and introducing a separate immutable bytes type; and a mostly corresponding mutable bytearray type, both of which represent arrays of bytes;[25]

Removing backward-compatibility features, including old-style classes, string exceptions, and implicit relative imports.

A change in integer division functionality. (In Python 2, 5 / 2 is 2. In Python 3, 5 / 2 is 2.5, and 5 // 2 is 2).

Subsequent releases in the Python 3.x series have included additional, substantial new features; all ongoing development of the language is done in the 3.x series.