Since Django doesn't yet support Python 3.x, I'm using Python 2.7. However, I'd like to go ahead and start familiarizing myself with the new Python 3.x syntax as much as possible. Which leads me to the question:
I know that running python -3
will
Warn about Python 3.x incompatibilities that 2to3 cannot trivially fix.
However, I'm interested in getting used to Python 3.x syntax while still using Python 2.7.
For instance, it seems that I should be using the following imports to my code:
from __future__ import print_function from __future__ import unicode_literals from __future__ import division from __future__ import absolute_import
The above four __future__ import
statements are required as of Python 3.0, but not required in 2.7 as described in Python 2.7.3's documentation 27.11. Future Statement Definitions
What else?
# Python 2 and 3: forward-compatible from builtins import range for i in range(10**8): ... # Python 2 and 3: backward-compatible from past.
Python 3 has an easier syntax compared to Python 2. A lot of libraries of Python 2 are not forward compatible. A lot of libraries are created in Python 3 to be strictly used with Python 3. Python 2 is no longer in use since 2020.
Python 2.6 includes many capabilities that make it easier to write code that works on both 2.6 and 3. As a result, you can program in Python 2 but using certain Python 3 extensions... and the resulting code works on both.
Many modules these days get rewritten in a way that allows execution on both Python 2 and Python 3. This turns out to be not very hard at all, and in the future it will be very easy to just drop Python 2 support.
Take a look at the six module that helps with this task, encapsulating many of the differences in a convenient way:
Six provides simple utilities for wrapping over differences between Python 2 and Python 3.
Its website (and of course, code) lists a lot of ways to make this possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With