Python module _ future __

Source: Internet
Author: User

Today, when I was studying Python Cookbook, I found a syntax from _ future _ import division, which is very strange. I searched the internet for the name of _ future, it turns out to be a very useful module.

For more information, see here. According to the official explanation, at least ensure that Python versions earlier than 2.1 can run some new language features normally. You need to use the statement 'from _ ure _ import *'. For example:

# Enable nested scopes in Python 2.1.
From _ future _ import nested_scopes

If you use this statement, it must be the first statement of the module or program. In addition, the features in the '_ ure _' module will eventually become part of the Python language standard. By that time, the '_ future _' module will no longer be required.

More examples:

1. There is also a _ ure _ import in Python 2.6 to convert all string texts into Unicode strings. This means that the \ u escape sequence can be used to contain Unicode characters.

From _ future _ import unicode_literals

S = ('\ u751f \ u3080 \ u304e \ u3000 \ u751f \ u3054'
'\ U3081 \ u3000 \ u751f \ u305f \ u307e \ u3054 ')
Print len (s) #12 Unicode characters

2. Python 2.6 can use import _ ure _ to remove print from the language syntax so that you can use the function form. For example:

From _ future _ import print_function
Print ('# of entries', len (dictionary), file = sys. stderr)

3. Integer Division

Python 2.5: 23/6 # Get 3
After from _ future _ import division:
23/6 # Get 3.8333333333333335

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.