This section describes the _ future _ module and Python _ future _ module in python _

Source: Internet
Author: User
Tags floor division integer division

This section describes the _ future _ module and Python _ future _ module in python _

Each new version of Python adds new features or makes some changes to the original features. Some changes are not compatible with the old version, that is, the code that runs normally in the current version may not work in the next version.

From Python 2.7 to Python 3. x has some incompatible changes, such as 2. the string in x uses 'xxx' to represent str, and the Unicode string uses u 'xxx' to represent unicode. in x, all strings are considered unicode. Therefore, writing u 'xxx' and 'xxx' are exactly the same, while in 2. str expressed as 'xxx' in x must be written as 'xxx' to represent a "binary string ".

Directly upgrading the code to 3.x is relatively false, because there are a lot of changes that need to be tested. On the contrary, you can first test some 3.x features in some code in version 2.7. If there is no problem, it is no longer necessary to port it to 3.x.

Python provides the _ ure _ module to import the features of the next version to the current version, so we can test some features of the new version in the current version. Example:

To adapt to the new String Representation Method of Python 3.x, in code 2.7, you can use unicode_literals to use the new syntax of Python 3.x:

# still running on Python 2.7from __future__ import unicode_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)

Note that the above Code is still running in Python 2.7, but the result shows that the 'A string' with the u prefix removed is still a unicode, B 'a string' with the prefix B is changed to str:

$ python task.py'xxx' is unicode? Trueu'xxx' is unicode? True'xxx' is str? Falseb'xxx' is str? True

In this case, Division is also used. In Python 2.x, there are two Division cases. If the result is an integer division and the result is still an integer, the remainder will be thrown away. This division is called "floor division ":

>>> 10 / 33

To perform an exact division, you must change one of the numbers to a floating point number:

>>> 10.0 / 33.3333333333333335

In Python 3. x, all Division operations are precise division, and the floor division is represented:

$ python3Python 3.3.2 (default, Jan 22 2014, 09:54:40) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> 10 / 33.3333333333333335>>> 10 // 33

If you want to use Python 3. x division directly in Python 2.7 code, you can use the division of the _ ure _ module:

from __future__ import divisionprint '10 / 3 =', 10 / 3print '10.0 / 3 =', 10.0 / 3print '10 // 3 =', 10 // 3

The result is as follows:

10 / 3 = 3.3333333333310.0 / 3 = 3.3333333333310 // 3 = 3

Summary

Because Python is an open-source and free development language promoted by the Community and is not controlled by commercial companies, Python improvements are often radical and incompatible. To ensure smooth transition to the new version, Python provides the _ ure _ module, allowing you to test some features of the new version 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.