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:
- # 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 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.
- 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
The above is our introduction to the Python future module.