Python future use

Source: Internet
Author: User

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.

There are some incompatible changes from Python 2.7 to Python 3.x, such as a string in 2.x ‘xxx‘ representing Unicode with a str,unicode string, u‘xxx‘ and in 3.x all strings are considered Unicode, so write u‘xxx‘and ‘xxx‘ is exactly the same, and in 2.x to ‘xxx‘ represent the Str must be written to b‘xxx‘ represent "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 __future__ modules to import the next new version of the feature into the current version, so we can test some new versions of the features in the current version. Examples are as follows:

There are division operations. In Python 2.x, there are two cases of 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 / 33

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

>>> 10.0 / 33.3333333333333335

In Python 3.x, all divisions are exact divisions, and the floor is represented by the addition of // :

 $ python3python 3.3.2 ( Default, Jan 22 2014, 09:54:< Span class= "number" >40) [GCC 4.2.1 Compatible Apple LLVM 5.0 (Clang-500.2.79)] on Darwintype  "Help",  "credits" or  "license" for more information.>>> Span class= "number" >10/33.3333333333333335>>> 10 //33         

If you want to use the Python 3.x division directly in Python 2.7 's code, you can pass __future__ the module's division implementation:

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

 

Python future use

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.