I can't move to Python3 easily, so let's consider compatibility 2-3

It's about time to talk about what to do with Python 3 migration, so I investigated a little.

Python2 support until 2020

Support for the Python 2 series has been officially announced until 2020. (It seems that there is no 2.8) Regardless of whether your current Python product is up and running until 2020 It's about time you should consider migrating to Python3.

First, a little enumeration of how it will change

Python2.X Python3.X Does 3 system work with 2 system notation?
print 'xxx' print('xxx') ×
xrange(1, 10) range(1, 10) ×
except Error, V except Error as V ×

range seems to be the behavior of xrange in 3 series.

It doesn't mean that the transition from 2nd system to 3rd system will change so much. It's just a small syntax change. (Check if the external library supports it)

Migration or compatibility

Even if it is out of support, it must be operated up to that point, and it is inefficient to implement it separately for 2nd and 3rd systems. Therefore, it is not possible to cut off the 2nd system and make a transition, and it is realistic to find a way to coexist with the 3rd system. Fortunately, in Python, 2nd and 3rd series can live together.

In case of migration

Since it's a big deal, the migration tool is also 2to3 2to3 is available as a batch conversion library. It's built into Python so you don't have to install it externally.

It seems that it will arrange the syntax and import as appropriate to match the syntax of Python3 series. 2to3 does not have the function to convert from 3 series to 2 series. But there is also a third-party library called 3to2. (Are you there?) https://pypi.python.org/pypi/3to2

Stay compatible

Here's what you can do to maintain compatibility.

Future with 3 series notation in 2 series

Execution environment 2 system can be used for 3 system notation. http://docs.python.jp/2/library/future.html

You will be able to write with print ().

from __future__ import print_function
print ('hoge')

Writing Unicode as u'hogehoge' eliminates the need for u.

>>> from __future__ import unicode_literals
>>> 'Japanese'
u'\u65e5\u672c\u8a9e'
>>> u'Japanese'
u'\u65e5\u672c\u8a9e'

Import has absolute import priority (difficult to explain)

from __future__ import absolute_import

There are three or four more, but they are omitted because they are not so relevant for Python 2.6 and above.

six There is also a library for compatibility. It's six. 2 * 3 = 6 and it seems to be six. It's easy to remember. This is an external library. http://hhsprings.bitbucket.org/docs/translations/python/six-doc-ja/

pip install six

To use it, just import it and use it for the incompatible parts of the 2nd and 3rd series. It is used internally according to the Python version at runtime.

For example, in the case of character strings

Python2 Python3 six
basestring str string_types
unicode str text_type
str bytes binary_type

iterable (collection) The iterable system looks a lot like that, so if you look at the dict that you might use often,

Python2 Python3 six
iterkeys keys iterkeys
itervalues values itervalues
iteritems items iteritems

The notation is similar to the 2nd system.

A good number of built-ins can be absorbed by six.

In the first place

There are 2-3 compatible cheat sheets! http://python-future.org/compatible_idioms.html

Is it easy to make the transition?

I've found compatibility, but finding and fixing the corresponding parts one by one is a ridiculous effort. I want a batch conversion library. Is there?

was! https://github.com/python-modernize/python-modernize It seems that it will be converted using six as well, so it seems quite promising. When I looked into the source a little, there were six and future, so I think they will be converted into a nice feeling! ?? I would like to try it at another time. (I didn't have enough time this time. I'm really sorry.) (If you have tried it, this is your chance to write an article first!)

The good thing about Python 3

It takes a lot of time to migrate, but there are some good things about Python3.

Type hint

http://docs.python.jp/3/library/typing.html You can specify the type when defining a function or class.

def greeting(name: str) -> str:
    return 'Hello ' + name

Unlike the compile language, it cannot be checked at runtime, It is quite important to know the argument type immediately during development in order to prevent bugs.

Ordered dictionary

It is a dictionary that can be retrieved in the order in which it was entered. Previously, I remember putting the sort order in value and then sorting it, so it seems to be useful.

>>> from collections import OrderedDict
>>> od = OrderedDict()
>>> od['a'] = 1
>>> od['b'] = 2
>>> od['c'] = 3
>>> for k, v in od.items():
...   print(k, v)
... 
a 1
b 2
c 3

>>> od2 = OrderedDict()
>>> od2['c'] = 3
>>> od2['b'] = 2
>>> od2['a'] = 1
>>> for k, v in od2.items():
...   print(k, v)
... 
c 3
b 2
a 1

It seems to be even faster from Python 3.5!

Furthermore, it seems that asynchronous generators will be implemented in Python 3.6, and the dream will spread. Let's support Python3 for new features as well as the negative motive of support deadline!

Recommended Posts

I can't move to Python3 easily, so let's consider compatibility 2-3
I want to easily implement a timeout in python
I want to write in Python! (2) Let's write a test
Let's use python janome easily
[Python] I tried to implement stable sorting, so make a note
I tried to easily detect facial landmarks with python and dlib
I tried to touch Python (installation)
Let's move Cython and Numba easily
I can't install python3 with pyenv-vertualenv
I can't remember Python regular expressions
[Introduction to Python] Let's use pandas
I can't install scikit-learn in Python
[Introduction to Python] Let's use pandas
Easily post to twitter with Python 3
I want to debug with Python
[Introduction to Python] Let's use pandas
I tried to move the ball
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried to implement permutation in Python
I made Othello to teach Python3 to children (4)
I installed Python 3.5.1 to study machine learning
I made Othello to teach Python3 to children (2)
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I want to use jar from python
I wanted to solve ABC160 with Python
I want to build a Python environment
[Introduction to Python] Let's use foreach with Python
I can't debug python scripts in Eclipse
I want to analyze logs with Python
I want to play with aws with python
I made Othello to teach Python3 to children (5)
I tried to implement ADALINE in Python
I wanted to solve ABC159 in Python
I tried to implement PPO in Python
[Python] I tried to calculate TF-IDF steadily
I tried to touch Python (basic syntax)
What I was addicted to Python autorun
I made Othello to teach Python3 to children (3)
I made Othello to teach Python3 to children (1)
I wanted to solve ABC172 with Python
Directory move tool to Python package (pycd)
What I did to save Python memory
Why can't I install matplotlib in python! !!
I tried to create a class that can easily serialize Json in Python
I want to send Gmail with Python, but I can't because of an error
I tried to explain what a Python generator is for as easily as possible.
I tried to easily visualize the tweets of JAWS DAYS 2017 with Python + ELK
I tried to easily create a fully automatic attendance system with Selenium + Python
I was able to mock AWS-Batch with python, moto, so I will leave it