Using __future__ to achieve transitions from python2.7 to python3.x

Source: Internet
Author: User

Reference Link: http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 001386820023084e5263fe54fde4e4e8616597058cc4ba1000

There are some incompatible changes in python2.7 to 3.x, and if you upgrade the Python version directly, it will result in some old Python code not executable; In order to solve this problem, the Python community has developed the __FUTURE__ module;

1. String compatibility

In python2.7, any created string object, such as A= ' str1 ', is represented by default as a string type, and if it needs to be converted to a Unicode code, you need to precede the string with u, that is, a=u ' str1 ';

In python3.x, any string created by default is Unicode code,

From __future__ import unicode_literalsprint ' \ ' 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) results: ' xxx ' is Unicode? Trueu ' xxx ' is Unicode? True ' xxx ' is str? Falseb ' xxx ' is str? True

2. Division Compatible

Python2 Division of the floor, the default is to keep only the integer part, if you want to retain the fractional part, you need to

Using __future__ to achieve transitions from python2.7 to python3.x

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.