New versions of Python introduce new features, but in fact these features already exist in the previous version. To "try out" a new feature, you can do so by importing some of the features of the __future__ module.
For example, the integer division operation of Python 2.7 is still an integer:
>>> 10/33
However, Python 3.x has improved the division of integers, "/" In addition to the floating-point number, "//" is still an integer:
>>> 10/33.3333333333333335>>> 10//33
To introduce the 3.x division rule in Python 2.7, import the __future__ Division:
>>> from __future__ import division>>> print 10/33.3333333333333335
When an attribute of the new version is incompatible with the old version, the feature will be added to the __future__ in the old version so that the old code can test the new feature in the old version.
Task
In Python 3.x , string unification is Unicode, no prefix uis required, and str stored in bytes must be prefixed with b. Use __future__ 's unicode_literals to write Unicode strings in Python 2.7.
-
- ? What's going on?
-
-
Using the From __future__ import unicode_literals will bring the Python 3.x Unicode rules into the Python 2.7 .
Reference code:
From __future__ import UNICODE_LITERALSS = ' am I a Unicode? ' Print isinstance (s, Unicode)
Python uses __future__