py: from __future__ import introduction
__future__
The role of the module
__future__
Modules are used to introduce functionality of later versions of py in the current version of the py compiler- The statement must be written on the first line
- This module is only supported in versions after Python 2.1
Each version of python will make some improvements, and most of them are not backward compatible. At this time, the old code needs to be modified to meet the requirements of the new Python version. But if the code of the entire project is migrated, it is very troublesome. When we need to use some features of the new version, we may wish to introduce the feature on the basis of the original code, so that the amount of code changes can be reduced. At this time, we need to use __future__
modules, which support the introduction of newer versions in the old Python. function. So in a sense, Python is a backwards compatible language.
How to use
from __future__ import xxx
print_function
In py2, print is not a function, and parentheses are not required to use it.
But in py3, print is a function, the calling method is the same as the general py function, and parentheses need to be added.
print 'error'
------------
OUT:
File "<ipython-input-2-58dc6da12e84>", line 1
print 'error'
^
SyntaxError: Missing parentheses in call to 'print'
If you want to make the print in py2 also need to add parentheses, then you need to write the following statement to introduce the print_function in py3
from __future__ import print_function
absolute_import
First we assume that your Python project is structured as follows:
pak/
pak/main.py
pak/str.py
pak/mystr.py
pak/xml.py
Three Formats of Package References
absolute_import
is called if you write the following in your main.py file:from pak import str import pak.str import mystr
Generally, it is an absolute reference, because your package name pak generally will not conflict
relative import
if so:import str import xml import mystr
You can't see the affiliation of the package. If your py environment is 2.4 or earlier, then it will first search for packages such as str, xml, and mystr from the folder where the current script main.py is located, so that It will conflict with the str and xml packages that come with the py system, thereby overwriting them.
In versions 2.5 and 2.6, you can usefrom __future__ import absolute_import
to solve this problem, which is to introduce absolute references from higher Python versions, thereby forcing the default absolute references.
Versions after 2.7 are absolute references by default.explicit relative reference
from . import str from . import xml from . import mystr
If you are too lazy to write the package name like an absolute reference, and you need to avoid the referenced package from overwriting the system default package, you can write it as above. If there are multiple levels of folders, then there are a few more points, but generally speaking, points above the second level are not recommended, that is, to
from .. import xxx
That's about it, adding a few more points will greatly affect the readability of the code.