Introduce the __future__ module in Python _python

Source: Internet
Author: User

Each new version of Python adds new features or changes to the original functionality. Some of the changes are not compatible with the old version, that is, the current version of the code to run normally, to the next version of the run may not be normal.

There are some incompatible changes from Python 2.7 to Python 3.x, such as strings in 2.x using ' xxx ' to denote str,unicode strings with u ' xxx ' for Unicode, and in 3.x, all strings are considered Unicode, so Write U ' xxx ' and ' xxx ' are exactly the same, and in 2.x the ' xxx ' str must be written as B ' xxx ' to represent the binary string.

To directly upgrade the code to 3.x is more aggressive, because there are a lot of changes need to test. Instead, you can test some of the 3.x features in part of the code in version 2.7, and if there is no problem, migrate to 3.x later.

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 release. Examples are as follows:

To accommodate the new string representation of the Python 3.x, the new syntax for Python 3.x can be used in the 2.7 version of the Code by Unicode_literals:

# still running on Python 2.7

from __future__ import unicode_literals

print ' \ xxx\ ' are 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)

Note that the above code still runs under Python 2.7, but the result shows that the ' a string ' that removes the prefix u is still a Unicode, and a B ' string ' with prefix B becomes str:

$ python task.py
' xxx ' is Unicode? True
u ' xxx ' is Unicode? True
' xxx ' is str? False
B ' xxx ' is str? True

A similar situation also has a division operation. In Python 2.x, there are two cases of division, in which the result is still an integer, and the remainder is thrown away, which is called "Floor division":

>>> 10/3
3

To do precise division, you must turn one of the numbers into floating-point numbers:

>>> 10.0/3
3.3333333333333335

In the Python 3.x, all Division is exact division, and the floor is used//represented by:

$ python3
Python 3.3.2 (default, out of 2014, 09:54:40) 
[GCC 4.2.1 compatible Apple LLVM 5.0 (clang-500.2.79)] O N Darwin
Type ' help ', ' copyright ', ' credits ' or ' license ' for the more information.
>>> 10/3
3.3333333333333335
>>> 3
3

If you want to use Python 3.x division directly in the Python 2.7 code, you can implement it through the division of the __FUTURE__ module:

From __future__ Import Division

print ' 10/3 = ', 10/3
print ' 10.0/3 = ', 10.0/3
print ' 10//3 = ', 10// 3

The results are as follows:

10/3 = 3.33333333333
10.0/3 = 3.33333333333
10//3 = 3

Summary

Since 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. Python, in order to ensure that you can successfully transition to the new version, specifically provides the __FUTURE__ module, allowing you to test the new version of some features in the old version.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.