Python uses __future__

Source: Internet
Author: User

New versions of Python introduce new features, but in fact these features already exist in the previous version. To "try out" a new feature, you can do so by importing some of the features of the __future__ module.

For example, the integer division operation of Python 2.7 is still an integer:

>>> 10/33

However, Python 3.x has improved the division of integers, "/" In addition to the floating-point number, "//" is still an integer:

>>> 10/33.3333333333333335>>> 10//33

To introduce the 3.x division rule in Python 2.7, import the __future__ Division:

>>> from __future__ import division>>> print 10/33.3333333333333335

When an attribute of the new version is incompatible with the old version, the feature will be added to the __future__ in the old version so that the old code can test the new feature in the old version.

Task

In Python 3.x , string unification is Unicode, no prefix uis required, and str stored in bytes must be prefixed with b. Use __future__ 's unicode_literals to write Unicode strings in Python 2.7.

? What's going on?

Using the From __future__ import unicode_literals will bring the Python 3.x Unicode rules into the Python 2.7 .

Reference code:

From __future__ import UNICODE_LITERALSS = ' am I a Unicode? ' Print isinstance (s, Unicode)

Python uses __future__

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.