Explanation of common examples of Python ure Module

Source: Internet
Author: User
Tags integer division

There are many useful modules in the Python programming language. These modules can help us easily meet many functional requirements in actual use. We will learn more about the Python future module here today.

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:

 
 
  1. # Enable nested scopes in Python 2.1  
  2. 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 Python future module will no longer be needed.

More examples of the Python ure module:

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.

 
 
  1. from __future__ import unicode_literals  
  2. s = ('\u751f\u3080\u304e\u3000\u751f\u3054'  
  3. '\u3081\u3000\u751f\u305f\u307e\u3054')  
  4. 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:

 
 
  1. from __future__ import print_function  
  2. print('# of entries', len(dictionary), file=sys.stderr) 

3. Integer Division

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

The above is our introduction to the Python future module.

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.