Transferred from: http://www.jb51.net/article/65030.htm
each new version of Python adds new features or makes some changes to the original functionality. Some of the changes are incompatible with the old version, which is the code that runs normally in the current version, and may not work properly to the next version. From Python2.7 To Python 3.x there are some incompatible changes, such as the string in 2.x'XXX'Represents the Str,unicode string with u'XXX'Represents Unicode, and in 3.x, all strings are treated as Unicode, so the write U'XXX'And'XXX'is exactly the same, and in 2.x'XXX'The Str represented must be written in B'XXX', which means "binary string". To directly upgrade the code to 3.x is more aggressive, because there are a lot of changes that need to be tested. Instead, you can test some 3.x features in a subset of the code in version 2.7, and if there are no problems, then porting to 3.x is not too late. Python provides the __future__ module to import the next new version of the feature into the current version, so we can test some of the features of the new version in the current version. Examples are as follows: In order to adapt to PythonA new representation of the 3.x string, in the 2.7 version of the code, can be used by unicode_literals to use Python 3The new syntax for. x: #still running on Python 2.7 from __future__ Importunicode_literalsPrint '\ ' xxx\ ' is Unicode?', Isinstance ('XXX', Unicode)Print 'u\ ' xxx\ ' is Unicode?', Isinstance (U'XXX', Unicode)Print '\ ' xxx\ ' is str?', Isinstance ('XXX', str)Print 'b\ ' xxx\ ' is str?', Isinstance (b'XXX', str) Notice that the above code is still in Python2.7 Run, but the results show that the prefix U is removed'A String'is still a Unicode, and B with prefix b'A String'has become str:$ python task.py'XXX' isUnicode? Trueu'XXX' isUnicode? True'XXX' isstr? Falseb'XXX' isstr? True similar cases have division operations. In Python2in. x, there are two cases for division, and if an integer is divided, the result is still an integer, and the remainder is thrown away, which is called "floor division":>>> 10/33to do the exact division, you must turn one of the numbers into a floating-point number:>>> 10.0/33.3333333333333335and in Python3.x, all divisions are precision division, the floor is removed//indicated that:$ Python3python3.3.2 (default, Jan 22 2014, 09:54:40) [GCC4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on Darwintype" Help","Copyright","credits" or "License" forMore information.>>> 10/33.3333333333333335>>> 10//33If you want to be in Python2.7 of the code directly using Python 3The division of. x can be achieved through the division of the __FUTURE__ module: from __future__ ImportDivisionPrint '10/3 =', 10/3Print '10.0/3 =', 10.0/3Print 'Ten//3 =', 10//3The results are as follows:10/3 = 3.3333333333310.0/3 = 3.3333333333310//3 = 3summary Because Python is a community-driven open source and free development language that is not controlled by commercial companies, Python's improvements tend to be more aggressive and incompatible situations occur. In order to ensure that you have a smooth transition to the new version, Python specifically provides the __future__ module, which lets you experiment with some features of the new version in the old version.
"Python" __future__ module