It's about time to talk about what to do with Python 3 migration, so I investigated a little.
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)
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.
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
Here's what you can do to maintain compatibility.
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.
There are 2-3 compatible cheat sheets! http://python-future.org/compatible_idioms.html
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!)
It takes a lot of time to migrate, but there are some good things about Python3.
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.
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